Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Errors and failure cases

When a token exchange 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 token 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 API has to decide what to tell it. An exchange fails like any other token request, with a JSON error from the token endpoint, usually with status 400; a client that tried to authenticate with HTTP 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:

Token exchange error codes
ErrorWhat it means in an exchange
invalid_requestThe 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_clientClient authentication failed.
unauthorized_clientThe client authenticated, but is not permitted to use token exchange.
unsupported_grant_typeThe server does not support token exchange at all.
invalid_targetThe server will not issue a token for the requested resource or audience.
invalid_scopeThe 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 code or refresh token, 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 token 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 authorization 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 server 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.

Three failures at the second hop. An expired token goes back to whoever holds a fresh credential behind it; a refusal that reports policy stops at the storage service and alerts its operators.

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: allowed

The 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.

Midway through a long request, an exchange fails with invalid_request because the printer's token has expired. What should the photo API do? 1 of 4

Choose one answer.

An exchange request times out with no response. Why is one retry reasonable here, when replaying an authorization code is not? 2 of 4

Choose one answer.

The storage service exchanges token B for an archive token, but token B expired five seconds earlier and the answer is invalid_request. The photo API's token A is still valid. What should the storage service do? 3 of 4

Choose one answer.

The archive must serve originals only to requests that began in the photo service's own apps. Where can it learn which client started the chain? 4 of 4

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity