Generate access token for salesforce using python

Refresh

April 2019

Views

9 time

0

I'm trying to extract a sales-force report using REST API using Python

The access_token and instance_URL are blank

I tried giving multiple request.post url's but none is working.. The 4 parameters I gave working and is correct

import requests
params = {
    "grant_type": "password",
    "client_id": "XXX.YYY", # Consumer Key
    "client_secret": "0000000000000000", # Consumer Secret
    "username": "[email protected]", # The email you use to login
    "password": "MyPasswordMySecurityToken" # Concat your password and your security token
        }
r = requests.post("https://test.salesforce.com/services/oauth2/token",params=params)
access_token = r.json().get("access_token")
instance_url = r.json().get("instance_url")
print("Access Token:", access_token)
print("Instance URL", instance_url)

Actual:-

Access Token: None Instance URL None

Expected :- actual values

1 answers

0

I have checked your code and it's working with correct configuration parameters

modify your code like that and check errors

sf_response = requests.post("https://test.salesforce.com/services/oauth2/token", params=params)
sf_response_json = sf_response.json()
if "error" in sf_response_json:
    print(sf_response_json.get("error"))
    print(sf_response_json.get("error_description"))
else:
    access_token = sf_response_json.get("access_token")
    instance_url = sf_response_json.get("instance_url")
    print("Access Token:", access_token)
    print("Instance URL", instance_url)

When incorrect pass+sectoken passed I got

invalid_grant

authentication failure

for invalid client id I got

invalid_client_id

client identifier invalid

for invalid client secret:

invalid_client

invalid client credentials