Requesting access to multiple resources
After printing Ava's book, the printer offers to send a preview of album 42 to people she chooses, which needs 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. and the sharing API. Resource indicators let Ava approve both in one 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. while the printer receives a separate 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. for each API, so no single token is valid at both.
One token for each API
The authorization covers two APIs, but the printer asks for its access tokens one API at a time. When it exchanges the code, it names only the photo API:
POST /token HTTP/1.1
Host: auth.photos.example
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=demo-code-21
&redirect_uri=https%3A%2F%2Fprinter.example%2Foauth%2Fcallback
&code_verifier=demo_verifier_for_the_two_api_attempt_21_2026
&resource=https%3A%2F%2Fapi.photos.example
&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer
&client_assertion=eyJhbGciOiJFUzI1NiIsImtpZCI6InByaW50ZXItMjAyNi0xMCIsInR5cCI6IkpXVCJ9...The printer authenticates with its signed client assertion, shortened here for display. The response carries a token for the photo API and a 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.:
{
"access_token": "demo-access-token-21",
"token_type": "Bearer",
"expires_in": 600,
"refresh_token": "demo-refresh-token-11",
"scope": "photos.read"
}The 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.'s audience is the photo API alone, and the server has reduced its scope to photos.read, because photos.share means nothing at the photo API. Narrowing a token's scope to what its recipient needs is sometimes called downscoping. It also protects Ava's privacy: the photo API does not learn from the token which other services she uses. Because the granted scope differs from the one requested, the response says so in its scope field.
The refresh token is different. It stays tied to the whole authorization, both resources and both scopes. When Ava chooses to send the preview, the printer uses it to ask for a token for the sharing API:
POST /token HTTP/1.1
Host: auth.photos.example
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token=demo-refresh-token-11
&resource=https%3A%2F%2Fshare.photos.example
&client_assertion_type=urn%3Aietf%3Aparams%3Aoauth%3Aclient-assertion-type%3Ajwt-bearer
&client_assertion=eyJhbGciOiJFUzI1NiIsImtpZCI6InByaW50ZXItMjAyNi0xMCIsInR5cCI6IkpXVCJ9...{
"access_token": "demo-access-token-22",
"token_type": "Bearer",
"expires_in": 600,
"refresh_token": "demo-refresh-token-12",
"scope": "photos.share"
}The new access token is for the sharing API and carries only photos.share. The photo service rotates refresh tokens, so the printer saves demo-refresh-token-12 in place of demo-refresh-token-11 before doing anything else, as The refresh token grant lesson described. The next token for either API comes from the new refresh token.
Why not one token for both
A token request may include several resource values, and a server could answer with one token whose audience lists both APIs. The Scopes and audiences lesson explained the danger: a token valid at both APIs lets a leak at the sharing API expose Ava's photo library. The resource indicators specification takes the same view. It encourages clients to name a single resource in each token request, warns that any API in a token's audience can use it at the others, and lets a server refuse requests that name more than one.
Scopes get harder to interpret as well. A request that names several resources and several scopes asks for every scope at every resource. photos.read has no meaning at the sharing API, and every scope in a JWTA JSON Web Token: a compact token made of Base64url-encoded parts that carries claims and is usually signed. Anyone holding it can decode and read the claims, so the signature shows who issued it but hides nothing. access token must mean something to the APIs in its audience. A server may reject a combination of resources and scopes it cannot make sense of with the error invalid_target.
Naming more than one resource in the authorization request has none of these problems. It describes what Ava approves, and no token is ever valid at both APIs.
Keeping tokens apart
The printer now holds two access tokens and one refresh token. It stores each access token with the API it was issued for and sends each one only to that API. When one expires, it refreshes with that API's resource value and replaces only that token.
Both APIs share the same refresh token, so the rule from The refresh token grant lesson still applies: only one refresh for the connection runs at a time, even when the photo token and the sharing token need replacing at the same moment.
The refresh token cannot reach further than the authorization behind it. If the printer later asked for a token for an API Ava never approved, the photo service would refuse with invalid_target, because its policy limits token requests to the resources she approved, or some of them. Access to a third API needs a new authorization, with a new consent screen that names it.
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
- Approve two APIs once and get one token per APIApprove the photo and sharing APIs in one authorization, take a photo API token from the code and a sharing API token from the refresh token, and see one token for both refused once the APIs use different managers.