Skip to article
TOOLS. SYSTEMS. BETTER DECISIONS.
Stack & Method

The journal

How to refresh an OAuth token

Exchange a refresh_token at the token endpoint, handle rotation and errors, and keep tokens on trusted servers.

Minimal editorial diagram of server side token exchange illustrating how to refresh an oauth token with authorization server returning a new access token in brand colors

Quick answer

Refresh an OAuth token by making a POST request to the authorization server’s token endpoint with grant_type=refresh_token, including the refresh_token and your client credentials if required. On success, use the returned access token (and new refresh token if provided) and handle failures like invalid_token by retrying once after refresh or prompting the user to reauthorize if the refresh fails (https://www.oauth.com/oauth2-servers/making-authenticated-requests/refreshing-an-access-token/, https://oauth.net/2/refresh-tokens/).

Illustration style diagram of HTTP POST to token endpoint with three panels headers body and response showing two form field chips representing grant_type and refresh_token how to refresh an oauth token

Exact HTTP exchange (minimal server-side)

Send a POST with Content-Type: application/x-www-form-urlencoded to the token endpoint. Include at least:

  • grant_type=refresh_token
  • refresh_token=YOUR_REFRESH_TOKEN
  • Client authentication as required (e.g., client_id and client_secret, or another method)

Example form body: grant_type=refresh_token&refresh_token=REFRESH_TOKEN&client_id=CLIENT_ID&client_secret=CLIENT_SECRET. The response contains a new access_token and expires_in, and may include a new refresh_token; if no new refresh_token is returned, keep using the existing one. If an API call fails with invalid_token, refresh and retry once (https://www.oauth.com/oauth2-servers/making-authenticated-requests/refreshing-an-access-token/).

Provider specifics to confirm first

Security practices: storage, rotation and fallback

Server-side refresh flow checklist

Minimalist 2D vector checklist showing secure token handling steps store in backend rotate tokens detect reuse revoke on compromise using Stack and Method brand colors how to refresh an oauth token

Concurrency and retries at scale

Concurrent workers can all try to refresh the same credential when a token expires, causing race conditions, especially with refresh-token rotation where only the first refresh succeeds. Coordinate refreshes per credential using a distributed lock or equivalent, re-read the stored credential after acquiring the lock, and write the updated access token, refresh token (if any), and expiry atomically. Preserve the existing refresh token if the provider omits it in the response. Use a short pre-expiry buffer to reduce in-flight expirations, but do not rely on buffering alone. On a 401 with invalid_token, route the request through the same locked refresh path and retry the original call at most once (https://prismatic.io/blog/how-do-i-request-oauth-tokens-at-scale/, https://www.oauth.com/oauth2-servers/making-authenticated-requests/refreshing-an-access-token/).

Common mistakes and how to fix them

Stack & Method logo

FAQs

Should I refresh preemptively or only on 401?

Both. Use a short pre-expiry buffer to minimize failures, but also detect invalid_token responses and refresh then retry once. Always handle the case where refresh fails and reauthorization is required (https://www.oauth.com/oauth2-servers/making-authenticated-requests/refreshing-an-access-token/).

Do refresh tokens expire?

Yes, but details vary. Some providers do not expose refresh token lifetimes to clients, while others document fixed TTLs and even return them in responses (for example, LinkedIn’s refresh_token_expires_in). Your app must handle unexpected refresh failures by prompting users to reauthorize (https://www.oauth.com/oauth2-servers/making-authenticated-requests/refreshing-an-access-token/, https://learn.microsoft.com/en-us/linkedin/shared/authentication/programmatic-refresh-tokens).

Related guides

Sources and further reading

Your privacy choices

Optional analytics helps us improve these guides. It stays off until you allow it. Privacy details