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()
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':'example@example.com'})
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