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 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. in a direct request to the photo service, and 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. makes the checks that only it can make.
Building the token request
The 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. reached the printer through Ava's browser, the front channel from How applications communicate. The access 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. does not. The printer's backend sends the code straight to the photo service's token endpoint over HTTPSHTTP protected by TLS, so traffic is encrypted and checked for changes while it travels. It protects the connection, but does not prove that the site at the other end is honest., 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 clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server.. The client secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones. 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_2026The body is form-encoded, the same name=value pairs joined by & that an HTML form sends, not JSONA text format for structured information, written as named values inside curly braces. APIs, tokens, and discovery documents all use it.. 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 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. code from the callback. code_verifier is the PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else. 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 HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. 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, Base64A way of encoding data as ordinary text characters so it can travel safely in headers, URLs, and JSON. Anyone can decode it, so it hides nothing; Base64url is a variant safe for URLs, used in JWTs. 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.
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-7A 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.
Try it in the Lab
- Build the printer's token request by handWrite the Basic header yourself, see the checks only the token endpoint can make, send the form-encoded request, read every field of the response and call the photo API with the token.