Errors and failure cases
When a token exchangeAn OAuth grant, RFC 8693, in which a client trades a token it holds for a new one, usually for another audience, with narrower access, or acting for someone. It lets a service call the next service without passing the original token along. fails, the failure lands in the middle of someone else's request, and a useful answer depends on knowing which part failed and not making matters worse by retrying. This lesson reads exchange errors, follows a subject tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. that expires partway through Ava's album, decides which failures to retry and which service in a chain can recover, and traces the chain without logging a token.
Reading the error
Most exchanges finish in a few milliseconds and nobody notices them. When one fails, the printer is waiting for Ava's album 42, and the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. has to decide what to tell it. An exchange fails like any other token request, with a JSONA text format for structured information, written as named values inside curly braces. APIs, tokens, and discovery documents all use it. error from the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., usually with status 400; a client that tried to authenticate with HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. Basic and failed receives 401 instead. All identifiers in these examples are fictional.
HTTP/1.1 400 Bad Request
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{
"error": "invalid_request"
}Token exchange reuses the token endpoint's error codes and adds one from Resource indicators:
| Error | What it means in an exchange |
|---|---|
invalid_request | The request is malformed, for example missing subject_token_type, or sending actor_token_type without an actor token. Also used when the subject or actor token is invalid or unacceptable: expired, revoked, issued for a different API, or from an issuer the server does not trust. |
invalid_client | Client authentication failed. |
unauthorized_client | The client authenticated, but is not permitted to use token exchange. |
unsupported_grant_type | The server does not support token exchange at all. |
invalid_target | The server will not issue a token for the requested resource or audience. |
invalid_scope | The requested scope is unknown, or more than the policy and the subject token allow. |
The first row covers a lot. The specification requires invalid_request whenever the subject or actor token is invalid for any reason or unacceptable by policy. If you know the other grants, you might have expected invalid_grant, the code for a bad authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. or refresh tokenA longer-lived credential that a client sends to the authorization server, never to an API, to get new access tokens without asking the person again., and some servers do answer a rejected subject token that way. Either code tells the client that the exchange it sent will not succeed as it stands. The response does not say which check failed, and an optional error_description is written for developers, not for program logic.
Tokens that expire mid-operation
The printer's token lasts ten minutes, until 09:10. Suppose that at 09:09:30 the printer asks for one of Ava's large albums, 240 photos. The photo API exchanges the token at once and receives a storage token that expires at 09:10:00, capped by the printer's token. At 09:10:05, with 60 files still to fetch, the storage token has expired, and a fresh exchange fails with invalid_request because the subject token has expired too.
The photo API cannot repair this, and should not work around it. Each shortcut recreates a problem token exchange exists to avoid. Finishing the work with its own client credentials token would read Ava's files with authority she never gave. Forwarding the printer's token to storage would be refused, as it should be.
The honest answer is an error the printer can act on. The photo API responds with status 401 and error="invalid_token" in its WWW-Authenticate header, as Using access tokens described, and the printer obtains a fresh access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. and asks again. A client that replaces tokens well ahead of expiry rarely meets this, but every client should handle it.
The photo API can make it rarer too. Before starting work that will plainly outlast the subject token, it can check how long that token has left and ask for a fresh one at the start, rather than failing halfway through. And where work genuinely continues after the caller has gone, such as preparing print-resolution files overnight, the deployment designs that authorizationDeciding whether a requested action is permitted, such as whether a signed-in account may delete a particular photo. Authentication says which account is asking; authorization decides what it can do. explicitly instead of stretching a request's token. The specification allows a refresh token in an exchange response for cases where the client needs access after the original credential has stopped being valid, and expects deployments to document when one is issued.
Retrying with care
Some failures are worth retrying. A timeout, a dropped connection or a 5xx status from the token endpoint can be temporary, and one or two retries with a growing delay are reasonable. Unlike an authorization code, the subject token is not used up by an exchange, as Following a token exchange showed, so a repeated exchange simply issues another short-lived token.
The error codes in the table are different. Each one reports a decision, and repeating the same request gets the same decision. A loop between the photo API and the token endpoint only adds load. A loop that tries other targets or scopes until something succeeds is worse, because it probes the server's policy.
Errors from the downstream service follow the same reasoning. If the storage service answers 401 with invalid_token because the storage token expired, the photo API can exchange again, once, if the subject token is still valid, and retry the call once. A 403 with insufficient_scope means the token lacks the access the call needs, and exchanging the same way again will not change the policy.
When an exchange fails, the downstream call does not happen. The photo API does not fall back to forwarding the original token, to its own client credentials, or to a cached token from another request: it fails closed and says so. What it tells the printer depends on whose problem it is. An expired printer token deserves a 401 the printer can fix. unauthorized_client or invalid_target means the photo service's own configuration is wrong, so the photo API answers with a server error and alerts its operators, rather than a 401 that would send the printer to replace a perfectly good token.
When the second hop fails
In the chain from Following a token exchange, the storage service exchanged token B for token C to fetch an original from the archive. A chain adds one rule to everything above: each service can only repair what it holds.
Suppose the storage service is slow and sends its exchange at 09:03:05. Token B expired five seconds earlier, so the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. answers invalid_request. Retrying with token B cannot help, and the storage service has nothing else to exchange: it never saw token A and holds no refresh token. So it answers the photo API's request with 401 and invalid_token, as it would if token B had expired before reaching it. The photo API still holds a valid token A, exchanges it once more for a fresh token B, and retries the storage request once. If token A had expired as well, the same reasoning would carry the failure one hop further back, to the printer, the only party that can refresh it.
Storage holds an expired token B
- Exchange answer
- invalid_request
- Fresh credential
- Token A at the photo API
- Passed up
- 401 invalid_token
Photo API exchanges A again
Token A has expired too
- Exchange answer
- invalid_request
- Fresh credential
- Only the printer
- Passed up
- 401 invalid_token
The printer gets a fresh token
Storage asks for archive.delete
- Exchange answer
- invalid_scope
- Fresh credential
- Would not help
- Passed up
- Server error
Operators alerted, no retry
Each service can only repair what it holds.
The refusals that report a decision behave at the second hop as at the first. If the storage service asks for archive.delete, the answer is invalid_scope, because the policy turns storage.read into archive.read and nothing more. If it names a service its policy does not reach, such as a billing API, the answer is invalid_target. Neither changes on a retry, and neither is something the photo API or the printer can fix, so the storage service answers with a server error and alerts its operators rather than passing a 401 back up the chain. Nor can a service skip a hop: the policy lets photo-storage exchange only tokens issued for the storage service, so even if token A somehow reached it, the exchange would be refused.
Logging the chain
When something fails several services deep, the first question is who was acting for whom. Each party can record its part without recording a single token:
Authorization server, 2026-10-01 09:02:00 UTC
event: token exchange
client: photo-api
subject: user-2048
subject token jti: demo-token-id-7
issued token jti: demo-token-id-storage-1
target: https://storage.photos.example
scope: storage.read
outcome: issued
Storage service, 2026-10-01 09:02:00 UTC
event: object read
request ID: demo-request-31
subject: user-2048
current actor: photo-api
token jti: demo-token-id-storage-1
object: photo-1187
outcome: allowedThe jti values link the records. A token used at the storage service leads back to the exchange that produced it, and from there to the printer's token behind it. A request ID passed along with each internal call lets the services' records be joined even when no token was issued, such as when an exchange fails. The photo API's own log ties the printer's request to both.
What stays out matters as much. Never log the tokens, the Authorization header or the body of an exchange request, because each contains a credential someone could use. A refused exchange is recorded the same way as a successful one, with its error code, client, subject and target, which is often all an operator needs to see that a policy or configuration has changed.
Recording where the request started
Records like these show who acted for whom at each hop, but in the two-hop chain one party is missing from the last token. Token C names photo-storage as its client and current actor, with photo-api nested inside act. The printer appears only in token A, as its client_id, and that claim names the client that requested each token, so it changes at every hop. The chain in act records only the services that exchanged tokens. In this deployment, the archive's operators find the printer by following jti values back through the authorization server's exchange records: token C to the second exchange, token B to the first, and token A to the printer.
That is enough for an investigation, but not for a decision the archive must make itself, such as serving archived originals only to requests that began in the photo service's own apps. For that, a deployment has to add the information to the token deliberately, and the design takes three choices.
First, a claim with a defined meaning. RFC 8693 registers no claim for the client that started a chain, so the deployment defines its own, documented for every service that reads it. Second, the authorization server sets the claim, copying it from token A at the first exchange and from the subject token at every later one, never from a request parameter, so the services in between cannot add, change or drop it. Third, recipients treat it as the server's statement about where the chain began, not as an actor: the archive still authorizes the current actor, and it may now also refuse a request whose origin it does not serve. Token C would then read:
{
"iss": "https://auth.photos.example",
"sub": "user-2048",
"aud": "https://archive.photos.example",
"client_id": "photo-storage",
"scope": "archive.read",
"origin_client_id": "photo-printer",
"iat": 1790845321,
"exp": 1790845380,
"jti": "demo-token-id-archive-1",
"act": {
"sub": "photo-storage",
"act": {
"sub": "photo-api"
}
}
}The claim has a cost. Every downstream service now learns which application Ava used, which is information it did not need before, so add it only where a service actually decides on it. Inside one platform, carrying context such as this through a long call chain is the job of a different design, the transaction tokens described in The problem token exchange solves.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Read token exchange errors and decide whether to retry, fix or alertProduce token exchange errors and decide whether to retry, fix or alert, retry once after a downstream 401, let a subject token expire, and link the records by jti without logging a token.