Sub topic 5
Reading the token response
This chapter assumes you know basic HTTP, can read JSON, and understand the three OAuth 2.0 roles (client, authorization server, resource server). You have already seen that every Salesforce flow ends with a request to the token endpoint. Here you will learn what comes back and which fields your code must keep.
What the response looks like
A successful request to /services/oauth2/token returns a JSON body with status 200. This example comes from the web server flow with the api and refresh_token scopes:
{
"access_token": "00D...!AQ4A...",
"refresh_token": "5Aep861...",
"signature": "k3Jx...=",
"scope": "refresh_token api",
"id_token": "eyJraWQ...",
"instance_url": "https://MyDomainName.my.salesforce.com",
"id": "https://login.salesforce.com/id/00D.../005...",
"token_type": "Bearer",
"issued_at": "1764579012982"
}The exact set of fields depends on the flow and the scopes granted. The JWT bearer and client credentials flows never return a refresh_token.
The fields that matter
| Field | Meaning | What to do with it |
|---|---|---|
| `access_token` | The bearer token for API calls. | Send it in the `Authorization` header. Treat it as a secret. |
| `instance_url` | The base URL of the org that issued the token. | Build every API URL from it. |
| `token_type` | Always `Bearer` for Salesforce. | Use it as the header prefix. |
| `refresh_token` | A long-lived token for getting new access tokens. | Store it securely. Present only if the app requested the `refresh_token` scope and the flow supports it. |
| `scope` | The scopes actually granted. | Compare it with what you requested. |
| `id` | An identity URL for the user and org. | Call it with the token to get user details. |
| `id_token` | A signed JWT with identity claims. | Present only if the `openid` scope was granted. |
| `issued_at` | Issue time in milliseconds since the Unix epoch. | Use it for logging, not for expiry. |
| `signature` | A base64 HMAC-SHA256 value over `id` and `issued_at`. | Verify it only if you need to check the response was not altered. |
Why instance_url matters
Do not hard-code a hostname for API calls. Use the instance_url from the response. With My Domain it is your org's own domain, and it can differ from the host you sent the token request to, for example when you log in at login.salesforce.com.
What is missing: expiry
The response has no expires_in field. Session lifetime is set in the org by the session timeout and by the connected app's policies. Your code cannot compute the expiry from the response. Instead, handle a 401 response with INVALID_SESSION_ID by getting a new token: use the refresh token in the web server flow, or repeat the token request in the JWT bearer and client credentials flows.
Parsing the response
This Python snippet keeps the fields you need and fails early on a bad response:
import requests
def request_token(url, data):
resp = requests.post(url, data=data, timeout=30)
body = resp.json()
if resp.status_code != 200:
raise RuntimeError(f"{body.get('error')}: {body.get('error_description')}")
return {
"access_token": body["access_token"],
"instance_url": body["instance_url"],
"refresh_token": body.get("refresh_token"), # may be absent
"scope": body.get("scope", ""),
}Error responses use status 400 and carry error and error_description instead of a token. A later chapter covers those errors.
Flow-by-flow differences
Exercise
Your integration uses the JWT bearer flow and the token response contains no refresh_token. Is something misconfigured? How should the integration get a new token when the old one stops working, and which field tells it where to send API calls?
Show answer
Nothing is wrong. The JWT bearer flow does not return a refresh token. When the access token stops working (a 401 with INVALIDSESSIONID), build and sign a new JWT assertion and post it to the token endpoint again. Send API calls to the instance_url field from the token response.