Salesforce REST API: authentication in Python

Français Français

The REST API let one interact with the entire data of the CRM (accounts, contacts, opportunities…). An authentication is required beforehand, an access token must be obtained.

The documentation indicates 3 distinct OAuth flows to authenticate: Web Server OAuth, User-Agent OAuth and Username-Password OAuth.

The first two flows can offer the advantage to also return a refresh token that can be used to extend the validity of the access token. But the third flow, Username-Password OAuth, is the easiest solution for data analysis purposes. I will use this one in this snippet.

Create an App

In any case, create a new “Connected App” in your Salesforce portal (prod or sandbox).

Go in “Setup”, open the “App Manager”. Then, create a “New Connected App”. Name your application. Tick the box “Enable OAuth Settings”. In “Selected OAuth Scopes”, make all scopes available. Type “http://localhost/” in “Callback URL”. Save.

At the end, you should get and note down the “Consumer Key” and the “Consumer Secret”.

Use the Username-Password OAuth Flow to authenticate

With the user’s credentials and the the security token, use this Python script to get an access token.

Make calls to the Salesforce REST API

After successfully getting an access token, one can make calls the Salesforce REST API to extract data (leads, opportunities, etc.) or to modify and create new records.

Very simple example: let’s extract the next closing opportunities thanks to a SOQL request (Salesforce Object Query Language).

print(json.dumps(sf_api_call('/services/data/v39.0/query/', {
    'q': 'SELECT Account.Name, Name, CloseDate from Opportunity where IsClosed = False order by CloseDate ASC LIMIT 10'
}), indent=2))

The result is a JSON easy to exploit.