Client credentials
Some requests have no person behind them at all: a scheduled job sends work to a partner's 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. on its own account. The client credentials grant gives 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. 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. that represents the client itself, which makes that client's credential, and the access it unlocks, worth keeping small.
A client acting for itself
Every evening, the printer sends the day's finished photo books to the print lab, the business that prints them. The print lab runs an API that accepts print jobs from partner applications. Nobody is at a browser when this happens: a scheduled job on the printer's backend collects the orders and submits them.
Every request so far carried access that Ava delegated. The printer read her photos because she approved it, and even the December refresh only carried that approval forward. Here there is no photo account and no one to ask. The printer needs access to the print lab's API as itself.
The client credentials grant covers this situation. The client authenticates to 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. with its own credentials and receives an 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. that represents the client, not a user.
That changes what the API is deciding. In the authorization code flowThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser., the photo API asked whether the printer could read Ava's photos, and the answer rested on her approval. Here, the print lab's API asks whether the printer, as a registered partner, may submit print jobs. The answer depends on what the print lab has allowed that partner to do, not on any person's approval.
Reading Ava's photos
- Who authenticates
- Ava, then the printer
- Who approves
- Ava
- Token represents
- The printer acting for Ava
- API decides from
- Ava's approval
Photo API reads her album
Submitting print jobs
- Who authenticates
- The printer only
- Who approves
- No person
- Token represents
- The printer itself
- API decides from
- What the print lab allowed
Print lab API takes the job
A browser app tries
- Who authenticates
- Nothing it can protect
- Who approves
- No person
- Token represents
- No token issued
- API decides from
- Nothing to decide
Refused at the token endpoint
Anyone holding the printer's secret can get tokens for everything the printer is allowed to do.
Because the client proves who it is with its own credentials, this grant is only for confidential clients. The print lab has registered the printer's order service as a client, and the printer keeps that client's secret on the backend that runs the scheduled job. An application running in Ava's browser or installed on her phone is a public clientAn OAuth client, such as code running in a browser or an app on a phone, that cannot keep a credential secret because the people using it can inspect it. It relies on protections such as PKCE instead of a client secret.: it has no credential of its own that it can protect, so it can't use this grant to act as itself.
Requesting a token
The printer's backend sends a direct request to the print lab's 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.. As in the code exchange, this example uses HTTPThe Hypertext Transfer Protocol, the rules browsers, apps, and servers use to send requests and responses on the web. Basic client authenticationChecking evidence that someone, or some application, controls an account, such as a password or a passkey. It establishes which account is being used, not what that account may do.: the header carries the 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. encoding of printer-orders:demo-only-not-a-real-secret, the client ID and secret joined by a colon.
POST /token HTTP/1.1
Host: auth.printlab.example
Authorization: Basic cHJpbnRlci1vcmRlcnM6ZGVtby1vbmx5LW5vdC1hLXJlYWwtc2VjcmV0
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&scope=print-jobs.writeAll domains, credentials and tokens in these examples are fictional.
Compare this with 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. exchange. There is no code, no redirect URI and no PKCE verifier, because there was no earlier browser visit to connect to. The client's authentication is the whole basis of the request.
The authorization server checks those credentials, confirms that this client is allowed to use the grant, and decides which of the requested scopes to issue. A successful response looks familiar:
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{
"access_token": "demo-lab-access-token-2",
"token_type": "Bearer",
"expires_in": 900,
"scope": "print-jobs.write"
}There is no refresh token, and RFC 6749, the OAuth 2.0 specification, says there shouldn't be one for this grant. The yearly calendar needed a refresh token because Ava wouldn't be there in December to approve access again. The order service has a different basis for access: it can authenticate as itself whenever it needs a new token, so a refresh token would only add a second long-lived credential that does nothing the client secret can't already do. The fifteen-minute lifetime is this example's choice, not a rule every service follows.
Keeping the access narrow
The printer now submits jobs with the token:
POST /print-jobs HTTP/1.1
Host: api.printlab.example
Authorization: Bearer demo-lab-access-token-2
Content-Type: application/json
{ "order": "book-5561", "product": "hardcover-a4" }A sensible client keeps the token until shortly before it expires and uses it for the whole evening's batch, rather than requesting a new token for every job. When a request is refused because the token has expired, it requests another one.
The print lab still decides which operations the printer may perform. A token issued to the printer doesn't prove that a particular customer approved a particular order. If that proof matters, it has to come from the printer's own order records and checks, not from a customer name added to the request body, which the printer could write for anyone.
That makes the client's credential especially valuable. Anyone holding the printer's secret can get tokens for everything the printer is allowed to do, at any time, with no person in the loop to notice or refuse. So the print lab grants the printer only the scopes its job needs, a token request that asks for anything else is refused, and the printer keeps its secret out of source code, logs and shared configuration. Secrets and signed assertions looks at a stronger way for a client to authenticate, and Rotating client credentials at how to replace a credential without interrupting the service that depends on 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
- Get a machine-to-machine token and keep its access narrowUse a confidential client with one exclusive scope, request a token with client credentials, read what it represents, then place a print order with it and see what it cannot do.