Newer Version Available

This content describes an older version of this product. View Latest

Analytics REST API Authentication

Salesforce uses the OAuth protocol to allow users of applications to securely access data without having to reveal username and password credentials.

The following sections introduce authentication and provide the steps you need to follow to set up authorization and connect to Analytics REST API using OAuth.

Understanding Authentication

Before making API calls using the Analytics API, you must authenticate the application user using OAuth 2.0. To do so, you’ll need to do the following:

  • Set up your application as a connected app in the Salesforce organization.
  • Determine the correct Salesforce OAuth endpoint for your connected app to use.
  • Authenticate the connected app user via one of several different OAuth 2.0 authentication flows. An OAuth authentication flow defines a series of steps used to coordinate the authentication process between your application and Salesforce. Supported OAuth flows include the following:

Set Up Authorization

Create a connected app in a Salesforce organization and enable OAuth. The client application uses the connected app to connect to Salesforce.

  1. In your Salesforce organization, from Setup, click Create | Apps , and in the Connected Apps section, click New to create a new connected app.
  2. Clients can use a connected app to sign in to any organization, even if the connected app isn’t defined in that organization.
  3. Enter a connected app name.
  4. Select Enable OAuth Settings.
  5. Enter a Callback URL. It must be secure: use https:// and not http://.
  6. Enter an OAuth scope. Select Access and manage your data (api) in addition to any other scope you want your connected app to allow access to.
  7. Click Save.
  8. The Consumer Key is created and displayed, and a Consumer Secret is created (click the link to reveal it).

Connect to Analytics REST API Using OAuth

Use OAuth to connect to Salesforce and get an access token. Pass the access token in requests to Analytics API.

This table maps the terms used in the connected app you created to the OAuth properties used in the examples. The OAuth 2.0 specification uses the word “client” instead of “consumer.”

Connected App Application Label

Value in Example

Consumer Key client_id Key
Consumer Secret client_secret

The instructions here use the username-password OAuth authentication flow. The username-password authentication flow passes the user’s credentials back and forth. Use this authentication flow only when necessary. No refresh token will be issued.

Note

To make a request to Salesforce, substitute values from your organization into these examples:

  1. Generate the access token.

    This cURL command generates an access token:

    1curl --form 
    2client_id=3MVG9PhR6g6B7ps4xDycwGrI4PvjVZvK9 
    3    --form client_secret=8870355475032095511
    4    --form grant_type=password 
    5    --form username=admin@seattleapps.com 
    6    --form password=1Lsfdc!
    7https://login.salesforce.com/services/oauth2/token
    To paste a multiline command into a Mac or Linux command line interface, escape each line with a backslash ( \ ) to indicate that the command continues on the next line. An escaped line looks like this:
    1curl --form client_id=3MVG9PhR6g6B7ps4xDycwGrI4PvjVZvK9 \
    To paste a multi-line command into the Windows Command Prompt, escape each line with a caret ( ^ ). An escaped line looks like this:
    1curl --form client_id=3MVG9PhR6g6B7ps4xDycwGrI4PvjVZvK9 ^

    Note

    The response includes the server instance and the access token:

    1{
    2"id":"https://login.salesforce.com/id/00Di0000000hT9uEAE/005i00000022uIbAAI",
    3"issued_at":"1302907727777",
    4"instance_url":"https://yourInstance.salesforce.com",
    5"signature":"5jcevY5fUai0lWntuSxkwBzWcvRjd01RCOkIBZpyGv0=",
    6"access_token":"00DD0000000FJ6T!AQkAQPde_DMF2vGzddfZmBRS95GojDbtA
    7
    8rKkgukAgZP0OVFYY5KkAqhLw9ejeKIlpJ3FgwGAWeRlBiWRt8mfXEuAZGbZNosk"
    9}
  2. To request an Analytics REST API resource, use the returned instance_url as the server instance. Pass the returned access_token as a Bearer token in the Authorization request header.
    1curl -X GET https://yourInstance.salesforce.com/services/data/v42.0/wave
    2  -H 'Authorization: Bearer 00DD0000000FJ6T!AQkAQPde_DMF2vGzddfZmBRS95Goj
    3  DbtArKkgukAgZP0OVFYY5KkAqhLw9ejeKIlpJ3FgwGAWeRlBiWRt8mfXEuAZGbZNosk'

Property

Value in Example

Server instance yourInstance.salesforce.com
client_id 3MVG9PhR6g6B7ps4xDycwGrI4PvjVZvK9
client_secret 8870355475032095511
grant_type password

The value of grant_type depends on the OAuth authentication flow you use

username admin@seattleapps.com
password 1Lsfdc!