jmcgrath207
jmcgrath207

Reputation: 2087

Passing Session ID from simple salesforce to requests

I am having trouble following the guide on in https://github.com/heroku/simple-salesforce to pass session ID information to requests. It seems that I can authenticate, but when I try queries with the API it give me a 'INVALID_SESSION_ID'. I am not sure what I am missing here and I assume it's maybe be my lack of understanding with APIs.

Any help is appreciated.

from simple_salesforce import Salesforce
import requests


session = requests.Session()
sf = Salesforce(instance='test.salesforce.com',session_id = '')
sf = Salesforce(username=username, password=password, security_token=security_token,session=session,sandbox=True)
response = session.get('https://test.salesforce.com/a4Z/o') #Returns a HTTP code 200
response = session.get('https://test.salesforce.com/services/data/v34.0/sobjects') #Returns a HTTP code 401
session.cookies.clear()

Upvotes: 1

Views: 3571

Answers (2)

user5051310
user5051310

Reputation:

Your instance argument is what appears to be the problem here. The library wants you to use your specific test instance of salesforce. So cs70.salesforce.com or cs17.salesforce.com and so on. When you log in to your sandbox from a browser from test.salesforce.com, upon successfully logging in, you are redirected to your instance (abXX.salesforce.com). Use that for instance.

Also, you don't need to have an "empty" Salesforce session variable (sf = Salesforce(instance='test.salesforce.com',session_id = ''))


Also, (and i know this is a 2 year old thread and i don't know what simple_salesforce was like back then but,) no need to use requests if you don't want to. Below is taken from the new github page. There are more examples on the site but this whole part of the answer is straying from the question.

Record Management To create a new 'Contact' in Salesforce:

sf.Contact.create({'LastName':'Smith','Email':'[email protected]'})

This will return a dictionary such as {u'errors': [], u'id': u'003e0000003GuNXAA0', u'success': True}

To get a dictionary with all the information regarding that record, use:

contact = sf.Contact.get('003e0000003GuNXAA0')

To get a dictionary with all the information regarding that record, using a custom field that was defined as External ID:

contact = sf.Contact.get_by_custom_id('My_Custom_ID__c', '22')

To change that contact's last name from 'Smith' to 'Jones' and add a first name of 'John' use:

sf.Contact.update('003e0000003GuNXAA0',{'LastName': 'Jones', 'FirstName': 'John'})

To delete the contact:

sf.Contact.delete('003e0000003GuNXAA0')

So, updated answer would only need the first 2 lines

from simple_salesforce import Salesforce

sf = Salesforce(username=username, password=password, security_token=security_token,session=session,sandbox=True)
# my_object = sf.Custom_Object__c.get('123456789012345')  # example use

Upvotes: 1

tadamhicks
tadamhicks

Reputation: 925

I don't know much about that lib, but you appear to be overwriting the instantiation of the sf variable. For instance, if you just did:

import requests
session = requests.Session()
response = session.get('https://test.salesforce.com/a4Z/o')

You would get a 200 because it is an open REST service for testing.

You're also not creating any values for the Salesforce() object to pass...As an example, look at the lib author's more full fledged example of using username and password for auth:

from simple_salesforce import Salesforce
import requests

session = requests.Session()
# manipulate the session instance (optional)
sf = Salesforce(
   username='[email protected]', password='password', organizationId='OrgId',
   session=session)

Obviously these are test values for the json, not actual usernames and values. You need to pass valid username and password to get this to work.

IF I were you I would leave off the first instantiation of your sf variable, put valid values in the second and try again.

Upvotes: 0

Related Questions