Einstein Platform Services

Generate an OAuth Token Using a Refresh Token

If you don't want to generate an access token using your private key, you can use a refresh token. A refresh token is a JWT token that never expires. You can use a refresh token only to generate an access token; you can't use it to make an authenticated API call.

This is useful in cases where the client making API calls doesn't have access to the private key. A third-party system can generate the refresh token and provide it to the client making API calls.

Warning

A refresh token never expires and is used to generate access tokens used to make API calls. Be sure to safeguard refresh tokens the same way you would any password.

To get an access token using a refresh token, you must first get the refresh token. Then you use the refresh token from then on to generate an access token.

Generate a Refresh Token

  1. Open the einstein_platform.pem file and read in the key contents.

  2. Create the JWT payload. The payload is JSON that contains:

    • sub—Your Einstein Platform Services username. You can find your username in the welcome email you receive after you get an account. If you signed up using Salesforce, your username is the email address associated with the org you signed up with.

    • aud—The API endpoint URL for generating a token.

    • exp—The expiration time in Unix time. This value is the current Unix time in seconds plus the number of seconds you want the token to be valid. For testing purposes, you can get the Unix time at Time.is.

    The JWT payload looks like this JSON.

{
  "sub": "<EINSTEIN_PLATFORM_SERVICES_USERNAME>",
  "aud": "https://api.einstein.ai/v2/oauth2/token",
  "exp": <EXPIRATION_SECONDS_IN_UNIX_TIME>
}
  1. Sign the JWT payload with your RSA private key to generate an assertion. The private key is contained in the einstein_platform.pem file you downloaded when you signed up for an account. The code to generate the assertion varies depending on your programming language. If you're doing manual testing, you can generate an assertion using jwt.io.
  1. Call the API and pass in the assertion along with the scope=offline parameter. You pass in all the necessary data in the -d parameter. Replace <ASSERTION_STRING> with the assertion you generated. This cURL command shows the call.
curl -H "Content-type: application/x-www-form-urlencoded" -X POST https://api.einstein.ai/v2/oauth2/token -d "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=<ASSERTION_STRING>&scope=offline"

The response looks similar to this JSON.

{
  "access_token": "SPFPQ5IBLB6DPE6FKPWHMIWW4MCRICX4M4KQXFQMI6THZXIEZ6QGNWNOERD6S7655LJAFWTRIKC4KGYO5G3XROMEOTBSS53CFSB6GIA",
  "refresh_token": "FL4GSVQS4W5CKSFRVZBLPIVZZJ2K4VIFPLGZ45SJGUQK4SS56IWPWACZ7V2B7OVLVKZCNK5JZSSW7CIHCNQJAO3TOUE3375108HHTLY",
  "token_type": "Bearer",
  "expires_in": "120"
}
  1. Store the refresh token.

Get an Access Token Using the Refresh Token

Now that you have a refresh token, you can use it to generate an access token that you can use to call the API.

  1. Call the /v2/oauth2/token endpoint and pass the refresh token along with these parameters.

  2. grant_type—Specify the string refresh_token.

  3. refresh_token—The refresh token you created.
  4. valid_for—Number of seconds until the access token expires. Default is 60 seconds. Maximum value is 30 days (2592000 seconds).
curl -H "Content-type: application/x-www-form-urlencoded" -X POST https://api.einstein.ai/v2/oauth2/token -d "grant_type=refresh_token&refresh_token=FL4GSVQS4W5CKSFRVZBLPIVZZJ2K4VIFPLGZ45SJGUQK4SS56IWPWACZ7V2B7OVLVKZCNK5JZSSW7CIHCNQJAO3TOUE3375108HHTLY&valid_for=60"

The response looks like this JSON.

{
  "access_token": "LA5JPHC6J2FOVPXVU36HW7WUF3GNNZC5PINC6NX272CEFCWGQNIBB3TSVEAUHD6SYZFED27YRDMRQDIEUNNRT7HXFJTNJCFU5DXNNOI",
  "token_type": "Bearer",
  "expires_in": "60"
}

Use the access token to make any authenticated API calls as long as the token is valid (it's not expired). When the access token expires, use the refresh token to generate a new one.

You can't use refresh token to generate another refresh token. The scope=offline parameter doesn't work for this call.

To delete a token, use the call to Delete a Refresh Token.

Updated 3 months ago

Generate an OAuth Token Using a Refresh Token


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.