Sub topic 9
Refreshing access tokens
This chapter covers how a web server flow app gets a new access token without sending the user through login again. It assumes you can already complete the web server flow from the previous chapters and that you are comfortable with curl and form-encoded POST requests.
Why access tokens expire
An access token is short-lived by design. Its lifetime follows the session timeout of the org or the connected app. If a token leaks, the damage is limited to that window.
A refresh token is a long-lived credential. Your app exchanges it for a new access token. The user does not see a login page.
Getting a refresh token
Salesforce returns a refresh token only if the app is allowed to ask for one. Two conditions must hold:
- The connected app's selected scopes include
refresh_token(shown in Setup as "Perform requests at any time"). You can useoffline_accessinstead. - The authorization request asks for that scope.
The authorization URL from the web server flow already includes it:
https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=<CONSUMER_KEY>&redirect_uri=https%3A%2F%2Fapp.example.com%2Foauth%2Fcallback&scope=api%20refresh_token&state=<RANDOM_STATE>&code_challenge=<S256_CHALLENGE>&code_challenge_method=S256If the scope is missing, the token response has no refresh_token field. Your app then has to send the user back through login when the access token expires.
Requesting a new access token
Send a POST to the token endpoint with grant_type=refresh_token:
curl -X POST https://login.salesforce.com/services/oauth2/token \
-d grant_type=refresh_token \
-d refresh_token=<REFRESH_TOKEN> \
-d client_id=<CONSUMER_KEY> \
-d client_secret=<CONSUMER_SECRET>Use your My Domain URL instead of login.salesforce.com if the org requires it.
The response has the same shape as the one from the code exchange. It contains a new access_token and the instance_url. It does not normally contain a new refresh_token. Keep using the one you already have.
sequenceDiagramThe refresh cycle
A common pattern is to refresh only when a call returns 401 with INVALID_SESSION_ID, then retry once. Retry only once. If the retry also fails, the refresh token is probably no longer valid.
When refresh fails
A refresh request that fails returns invalid_grant. Typical causes:
| Cause | What to do |
|---|---|
| The user or an admin revoked the token | Send the user through the web server flow again |
| The connected app's refresh token policy expired the token | Check the policy, then re-authorize |
| The token was issued for a different connected app | Use the consumer key that issued it |
| The token belongs to a sandbox but you called the production endpoint | Use `test.salesforce.com` for sandboxes |
Handling refresh tokens safely
- Store the refresh token encrypted, server side. Never put it in browser storage or in a mobile app's plain-text settings.
- Keep the consumer secret out of source code.
- Treat a refresh token like a password. Anyone who holds it, together with the client credentials, can mint access tokens.
Exercise
Your app stored a refresh token, but a refresh request returns invalid_grant. Name two likely causes and the fix.
Show answer
The token was revoked, or the connected app's refresh token policy expired it. The refresh token cannot be recovered, so send the user through the web server flow again to obtain a new one. Also check that you are using the consumer key that issued the token and the correct login host for the org type.