Reputation: 871
I am new to the salesforce API. Currently I am working on a helper method that would allow us to create a new user in our salesforce sandbox. One stumbling block I have hit at the moment is how to get an auth token from salesforce so I can pass that along with my other requests.
Here is the code [python] I have thus far:
def get_sf_auth_token(username, password):
req_username = 'users_name_here'
req_password = 'users_password_here'
req_url = 'https://na3.salesforce.com/services/oauth2/token'
payload = {"client_id":"COMNSUMER_KEY",
"client_secret":"CONSUMER_SECRET",
"username": req_username,
"password": req_password
}
sf_token = requests.post(req_url, data=payload)
print(sf_token.status_code)
print(sf_token.text)
Two values I am missing are CONSUMER_KEY and CONSUMER_SECRET. I do not know where these values come from.
When I send this request I get back the following:
400
{"error":"unsupported_grant_type","error_description":"grant type not supported"}
what is a grant type?
In general I just need help with this, any information is greatly appreciated.
Upvotes: 0
Views: 2318
Reputation:
I recommend using Simple-Salesforce python package, it does not require a consumer key. You just use your email address, password and security token.
from simple_salesforce import SFType
from simple_salesforce import Salesforce
sf = Salesforce(username='[email protected]', password='my_SF_password', security_token='my_Generated_token_from_Salesforce', client_id='This does not really matter')
Then a create would look like:
sf.Contact.create({'FirstName' : First, 'LastName' : Last, 'Newsletter__c' : Newsletter, 'Email' : Email1, 'Phone' : Phone, 'MailingStreet' : Street+" "+Apt, 'MailingCity' : City, 'MailingPostalCode' : Postal, 'AccountId' : "001U000001ZEB89"})
Upvotes: 0
Reputation: 1788
You'll need to add the grant_type
to your payload
. The value for the grant_type
key should be password
since you're using the username and password auth flow.
payload = {"client_id":"COMNSUMER_KEY",
"client_secret":"CONSUMER_SECRET",
"username": req_username,
"password": req_password,
"grant_type": "password"
}
You'll also want to be sure you append the security token to the end of the password in the payload. More info on this can be found here.
To get the client_id
and client_secret
, you need to create a connected app in the Salesforce UI. The steps for this process can be found here.
Upvotes: 3