Really early days for me learning to use the API as well as Python.

The code below Authenticates and passes back the token correctly - but I have no idea how to pass the session ID in the code after... I get an "invalid session ID error".

I simply need help with getting the "get" to authenticate and execute a simple request successfully. Thank you!

import requests
    consumer_key = "xxxx"
    consumer_secret = "xxxx"
    username = "xxxx"
    password = "xxxx"
    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",
    print r.content
    r = requests.get("https://ap2.salesforce.com/services/data/v36.0/wave")
    print r.text

Refer to Set up Authorization:

Once authenticated, every request must pass in the access_token value in the header. It cannot be passed as a request parameter.

Parse for access_token in request.content. Then use it in Authorization header:

import json, requests
body = json.loads(r.content)
token = body["access_token"]
url = body["instance_url"] + "/services/data/v36.0/limits"

r = requests.get(url, headers = {"Authorization":"Bearer " + token});
print r.content
| improve this answer | |
  • I'm getting an error : " tokens = r["access_token"] TypeError: 'Response' object has no attribute 'getitem'" – Dawid May 4 '16 at 10:03
  • my bad, content is a list of bytes, it should be loaded to json – Ilya Lepesh May 4 '16 at 18:07

For now it is necessary additionally to have security_token.

You can get it in SalesForce, using Lightning Theme:

  1. Click in the top right corner your profile icon -> Settings;
  2. In the tree select My Personal Information -> Reset My Security Token;
  3. Click Reset Security Token;
  4. Check your email for a new token.

In order to get client_id and client_secret (also known as consumer_key and consumer_secret) in SalesForce, using Lightning Theme it is necessary to:

  1. Click Gear Icon in the top right corner (next to Profile Icon) -> Setup;
  2. In the tree select PLATFORM TOOLS -> Apps -> App Manager;
  3. Click New Connected App in the top right corner;
  4. Fill in all required fields;
  5. Tick Enable OAuth Settings + enter any Callback URL + select needed Scopes;
  6. Click Save at the bottom, then Continue;
  7. On the new page you will see Consumer Key and Consumer Secret fields.

import requests

email = user@gmail.com
password = 'your_password'
security_token = 'admlnlkrsl'

client_id = 'Asdmlansdkjaduqoenaqonq234234'
client_secret = 'sdqlasdasmdpfk2093jdsajdiom'

your_instance = 'https://your_company.lightning.force.com'

payload = {
    'grant_type': 'password',
    'client_id': client_id,
    'client_secret': client_secret,
    'username': email,
    'password': password+security_token

request_token_url = "https://login.salesforce.com/services/oauth2/token"

r = requests.post(
    headers={"Content-Type":"application/x-www-form-urlencoded"}, data=payload)

body = r.json()
token = body['access_token']

url = your_instance + "/services/data/v45.0"
r = requests.get(url, headers = {"Authorization":"Bearer " + token})
print('response:', r.json())

SalesForce API Username-Password OAuth Authentication Flow

SalesForce EndPoints

| improve this answer | |

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.