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 to create an external client app and configure an OAuth flow before starting this task.

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

  1. Generate the access token.
    This cURL command generates an access token.
    1curl -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 that you’re using. This example uses the OAuth 2.0 Client Credentials Flow for Server-to-Server Integration, so the grant type is client_credentials. The response includes the server instance and the access token.
    1{
    2"access_token": "00DS7000000oxzo!AR8AQPXAhJNveaXDdPzbrtXqPykdVxBxtbLycpiyPmKyJm0Ku8gKKztulObJ1dhJU8Jt9W8k7FySsPAkvCnsadT_AbJTSW5T",
    3"signature": "nUFHwtfIGLKDTHcLWmDBG8frv0t+HQGl/iCZFIQUarE=",
    4"token_format": "opaque",
    5"instance_url": "https://instance_name.my.salesforce.com"
    6"id": "https://login.salesforce.com/id/00Dd000000XXXXXXX/005d000000XXXXX",
    7"token_type": "Bearer",
    8"issued_at": "1678833535086"
    9}
  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.
    1curl -X GET https://instance_name.my.salesforce.com/services/data/v67.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.