Beta

Create a tenant

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

Exchanging a code for tokens

Once the printer has matched the callback to its pending attempt, Ava's browser has done its part. The printer's backend now trades the code for an access token in a direct request to the photo service, and the token endpoint makes the checks that only it can make.

Building the token request

The authorization code reached the printer through Ava's browser, the front channel from How applications communicate. The access token does not. The printer's backend sends the code straight to the photo service's token endpoint over HTTPS, on the back channel, and the token comes back in the response to that same request. Her browser never sees it.

Here is the request, with the printer authenticating as a confidential client. The client secret is entirely fictional.

POST /token HTTP/1.1
Host: auth.photos.example
Authorization: Basic cGhvdG8tcHJpbnRlcjpkZW1vLW9ubHktbm90LWEtcmVhbC1zZWNyZXQ=
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=demo-code-7
&redirect_uri=https%3A%2F%2Fprinter.example%2Foauth%2Fcallback
&code_verifier=btl_training_verifier_for_one_attempt_only_2026

The body is form-encoded, the same name=value pairs joined by & that an HTML form sends, not JSON. Sending requests with curl sent bodies like this with -d. The line breaks here are only for reading.

Each field has one job. grant_type=authorization_code says which kind of exchange this is, because the same endpoint handles other grants. code is the authorization code from the callback. code_verifier is the PKCE verifier the printer kept in the pending attempt, sent for the first time now. redirect_uri repeats the callback address from the authorization request.

That repeated address does not send anyone anywhere. The token comes back in this response, not to the callback. The address is there so the token endpoint can compare it with the one the code was issued for. RFC 6749 requires it whenever the authorization request carried one, as the printer's did. OAuth 2.1 lets clients leave it out, because the PKCE verifier already ties the code to the attempt that asked for it, but servers still accept it from clients that send it.

The Authorization header carries the printer's client authentication: proof that the request comes from the registered printer, not just from someone who knows its client ID. This example uses HTTP Basic. The printer form-encodes its client ID, photo-printer, and its secret, joins them with a colon and Base64-encodes the result. The header above decodes to photo-printer:demo-only-not-a-real-secret. As Encoding and hashing showed, Base64 only changes how text is written, so anyone who sees the header can read the secret. HTTPS is what protects it on the way, and the printer never writes this header to a log.

HTTP Basic is only the method chosen for this example. A client can instead send its ID and secret as two more form fields, or prove itself with a short signed statement made with its private key, so no shared secret travels at all. A public client, like a browser app, has no secret to send: it puts its client_id in the body and relies on PKCE. Inventing a secret for it would not make it confidential. Client authentication methods compares these choices.

Checking the exchange

The browser part of the flow looked successful, but nothing has been issued yet, and the token endpoint now checks everything it can see. It authenticates the client. It looks up the code and confirms it was issued to this client, has not expired and has not been used before. It compares redirect_uri with the address the code was issued for, character by character. Finally it hashes the verifier and compares the result with the challenge stored with the code, as Proof Key for Code Exchange showed.

Each check covers a different kind of misuse. Someone who copied the code from Ava's browser history knows the printer's client ID, because it was in the authorization request, but cannot authenticate as the printer. Another registered client, even one the same company runs, cannot redeem a code issued to the printer. A code sent with a different return address, or with the verifier from another attempt, is refused even when the client is genuine.

A refused exchange comes back on the same connection, with HTTP status 400 and a JSON body naming the problem in error, often with an error_description written for developers. Failed client authentication is invalid_client, usually with status 401. A code that is unusable for any reason, whether expired, already used, issued to another client, sent with the wrong address or with the wrong verifier, is invalid_grant. A request that is malformed or missing a field, such as a JSON body or no redirect_uri, is invalid_request. Errors and denied access reads these in detail.

When every check passes, the server marks the code as used and answers:

HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache

{
  "access_token": "demo-access-token-7",
  "token_type": "Bearer",
  "expires_in": 600,
  "scope": "photos.read"
}

Cache-Control: no-store tells every cache along the way, including any proxy, not to keep a copy of a response that holds a credential. Pragma: no-cache says the same to older HTTP software that predates Cache-Control.

The body tells the printer how to use what it received. token_type is Bearer, so the token goes in an Authorization: Bearer header, and anyone holding it can use it, which is why it stays on the printer's backend. expires_in is the token's lifetime in seconds from now; ten minutes is this example's choice, not an OAuth rule. The printer stores the moment it ends, now plus 600 seconds, rather than the number itself, which means nothing an hour later. scope is what was actually granted. The server must include it when the grant differs from the request, and when it is left out, the grant is exactly what was requested.

There is no refresh_token here. A refresh token, which lets the printer get new access tokens later without Ava present, is optional, and this connection did not ask for one. The refresh token grant follows a connection that receives one.

This exchange runs directly between the printer backend and the token endpoint. The server checks the client and the code before issuing a token. The photo API then makes its own permission checks.

Using the result

The printer can now ask the photo API for Ava's photos, with the token in the header the token type named:

GET /photos HTTP/1.1
Host: api.photos.example
Authorization: Bearer demo-access-token-7

A successful exchange is the authorization server's decision, not the API's. The photo API still validates the token for its own use, checks that it has not expired and that its scope covers the operation, and enforces which photos belong to whom, as Trust boundaries described. A token for photos.read does not let the printer delete an album, however valid the exchange was. Enforcing access at an API covers those checks from the API's side.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The printer's authorization request carried a redirect_uri, so its token request repeats the same address. Why? 1 of 3

Choose one answer.

The token response holds access_token, token_type, expires_in and scope, but no refresh_token. Is the response broken? 2 of 3

Choose one answer.

Someone copies the printer's code from Ava's browser history and sends it to the token endpoint with the client ID photo-printer. Why is it refused? 3 of 3

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