Step Three: Connect to Connect REST API Using OAuth

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

Complete Step Two: Set Up Authorization and create a connected app before starting this task.

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

Connected App Application Label Example Label Example Value
Consumer Key client_id 3MVG9VwZxxW3bBQjhE6m21CG2nSS4WC.GNbZCtKeQxFH7218ns6xOLCNnJVKnZFTZWKzt2qwSFqt2taMt5mcr
Consumer Secret client_secret 8A4BE698BCA216718ECB0B1DCA294C69732EAD9472A1790F8FED38DD2E02B5FA

To make a request to Salesforce, substitute values from your org and connected app into these examples.

  1. Generate the access token.
    This cURL command generates an access token.
    curl -X POST https://instance_name.my.salesforce.com/services/oauth2/token -d 'grant_type=client_credentials' -d 'client_id=3MVG9VwZxxW3bBQjhE6m21CG2nSS4WC.GNbZCtKeQxFH7218ns6xOLCNnJVKnZFTZWKzt2qwSFqt2taMt5mcr' -d 'client_secret=8A4BE698BCA216718ECB0B1DCA294C69732EAD9472A1790F8FED38DD2E02B5FA'
    The value of grant_type depends on the OAuth authentication flow you’re using. For the user agent flow, use client_credentials. The response includes the server instance and the access token.
    {
    "access_token": "00DS7000000oxzo!AR8AQPXAhJNveaXDdPzbrtXqPykdVxBxtbLycpiyPmKyJm0Ku8gKKztulObJ1dhJU8Jt9W8k7FySsPAkvCnsadT_AbJTSW5T",
    "signature": "nUFHwtfIGLKDTHcLWmDBG8frv0t+HQGl/iCZFIQUarE=",
    "token_format": "opaque",
    "instance_url": "https://instance_name.my.salesforce.com"
    "id": "https://login.salesforce.com/id/00Dd000000XXXXXXX/005d000000XXXXX",
    "token_type": "Bearer",
    "issued_at": "1678833535086"
    }
  2. To request a Connect 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.
    curl -X GET https://instance_name.my.salesforce.com/services/data/v64.0/chatter/users/me -H 'Authorization: Bearer 00DS7000000oxzo!AR8AQPXAhJNveaXDdPzbrtXqPykdVxBxtbLycpiyPmKyJm0Ku8gKKztulObJ1dhJU8Jt9W8k7FySsPAkvCnsadT_AbJTSW5T'
If you get an unsupported_grant_type error, check the syntax of your cURL command. If you’re using Windows Command Prompt, replace single quotes (') in the examples with double quotes ("). For additional information about OAuth errors, see OAuth 2.0 Authorization Errors.