1

Hello according to this link :

Accessing the Salesforce API for the first time... using python

I have executed a python and got the following

C:\ProcesosCTM\Sales_Force>call python C:\ProcesosCTM\Sales_Force\token.py b'{"error":"invalid_grant","error_description":"authentication failure"}' [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

Can you help me with it ?

this is my python file and i think there must be anything lost with the token.

import requests

consumer_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
username = "xxxxxxxxxxxxxxxxxx"
password = "xxxxxxxxxxxxxxxxx"
payload = {
        'grant_type': 'password',
        'client_id': consumer_key,
        'client_secret': consumer_secret,
        'username': username,
        'password': password
           }
r = requests.post("https://login.salesforce.com/services/oauth2/token",
headers={"Content-Type":"application/x-www-form-urlencoded"},
data=payload)
print (r.content)
r = requests.get("https://ap2.salesforce.com/services/data/v36.0/wave")
print (r.text)
0

As you are getting "authentication failure" you may need to include your security token with the password.

...............
...............
password = "xxxxxxxxxxxxxxxxx"
security_token = "xxxxxxxxxxxx"
...............
...............
'password': password+security_token 
...................
...................
0

A few things to consider:

  • Do you have a user account setup in SalesForce for this username/password?
    • "grant_type": "password" uses user credentials to login
    • Other grant types use different endpoint URLs
  • Do you have IP restrictions turned on?
    • Then you need to send your password concatenated with your security token: [password][security token] as the value of password
  • Is this a sandbox instance of SalesForce?
  • Still not working?
    • Check that the client ID and secret match the values in SalesForce for your Connected App

Once you successfully authenticate, you need to use the instance_url you get back for requests. In your example it's ap2.salesforce.com but will be different for different instances:

r = requests.get("https://ap2.salesforce.com/services/data/v36.0/wave")
print (r.text)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.