Guide
OAuth 2.0 Grant Types: Which Flow to Use and How to Test Each One
A grant type is the way a client proves to an authorization server that it should get an access token. The client names it in the grant_type parameter of the token request, and each one carries a different kind of proof: a one-time code, the client's own credentials, an earlier refresh token, a signed assertion, or another token.
Updated November 20, 2026 · Filed under OAuth and SSO Flows
Picking the right grant is half the job. The other half is testing it, because each grant fails in its own way, and most of those failures come back from the token endpoint as the same two words: invalid_grant. This guide lists every grant in current use, which one fits which kind of client, how each flow runs, and the failure cases a test suite should force for each.
| Grant | grant_type value | Use it for | Status |
|---|---|---|---|
| Authorization code + PKCE | authorization_code | A user signing in through a browser: web apps, single-page apps, mobile and desktop apps | Current default (RFC 6749, RFC 7636) |
| Client credentials | client_credentials | A service calling an API as itself, with no user involved | Current (RFC 6749) |
| Refresh token | refresh_token | Getting a new access token after the old one expires, without asking the user again | Current (RFC 6749) |
| Device authorization | urn:ietf:params:oauth:grant-type:device_code | TVs, CLIs and other devices that can't open a browser or take typed input easily | Current (RFC 8628) |
| JWT bearer assertion | urn:ietf:params:oauth:grant-type:jwt-bearer | A workload presenting a signed JWT instead of a stored secret | Current (RFC 7523) |
| Token exchange | urn:ietf:params:oauth:grant-type:token-exchange | A service trading one token for another to call a downstream API on a user's behalf | Current (RFC 8693) |
| Implicit | None (token returned from the authorization endpoint) | Older browser apps | Deprecated: should not be used (RFC 9700) |
| Resource owner password | password | Apps that collected the user's password directly | Deprecated: must not be used (RFC 9700) |
Which grant fits your client
Start from who is asking for access and what the client can do, not from what the provider's quickstart happens to show.
- A person signs in and the app acts for them: authorization code with PKCE, whether the app has a backend or not.
- No person is involved and the service calls an API as itself: client credentials, or a JWT bearer assertion if the provider accepts one and you'd rather not store a long-lived secret.
- The device has no usable browser or keyboard: the device authorization grant.
- A backend already holds a user's token and needs a narrower or different one for a downstream service: token exchange, where the provider supports it.
- The access token expired and the user granted offline access: the refresh token grant.
Authorization code with PKCE
The client sends the user to the authorization endpoint with a code_challenge derived from a random code_verifier it keeps to itself. After the user signs in and consents, the authorization server redirects back with a short-lived code. The client then posts that code, the same redirect_uri and the original code_verifier to the token endpoint, which checks that the verifier matches the challenge before issuing tokens.
RFC 6749 recommends a code lifetime of no more than 10 minutes and says a code may be used only once; if it's presented twice, the server must refuse it and should revoke tokens already issued from it. PKCE (RFC 7636) was written for mobile apps, but the current security best practice (RFC 9700) requires it for public clients and recommends it for confidential ones too.
What to test:
- The happy path end to end, with the code, verifier and redirect URI all carried from one step to the next rather than hard-coded.
- Redeeming the same code twice: the second call should fail with
invalid_grant, and tokens from the first may be revoked. - A code redeemed after its lifetime, by advancing the simulator's clock instead of sleeping.
- A wrong or missing
code_verifier, and aredirect_urithat differs from the one in the authorization request: both areinvalid_grant. - A mismatched or missing
stateon the callback, which your client should reject before it ever calls the token endpoint. - The user denying consent, which comes back on the redirect as
error=access_denied.
Read more: Sequencing OAuth Grant Steps as Ordered Integration Test Contracts · OAuth Consent Screen Bypass Patterns in Automated Testing
Client credentials
The client authenticates with its own ID and secret (or a private key) and asks for a token in its own name. There is no user, no redirect and no consent screen, which makes it the easiest grant to automate and the easiest to over-scope.
RFC 6749 says a refresh token should not be included in this grant's response, so the client is expected to request a new access token when the old one expires.
What to test:
- A wrong secret or an unknown client ID:
invalid_client, often with HTTP 401. - A client that isn't allowed this grant:
unauthorized_client. - Asking for a scope the client was never granted:
invalid_scope. - Token expiry mid-job, and that your code fetches a fresh token instead of looking for a refresh token that was never issued.
- Rotated client secrets: the old secret should stop working when the provider says it does, not when your cache happens to clear.
Read more: PayPal OAuth 2.0 Client Credentials Flow for Server-Side Integrations
Refresh token
When an access token expires, the client posts the refresh token it was given earlier to get a new one. It can ask for the same scope or a narrower one, never a wider one. The server may also return a new refresh token, and when it does, RFC 6749 says the client must discard the old one.
That last rule is where most refresh bugs live. With refresh token rotation, which RFC 9700 names as one way to protect refresh tokens held by public clients, reusing an old refresh token is treated as a sign of theft and fails with invalid_grant. Some servers also revoke the whole token family when that happens. How long refresh tokens last, and whether they rotate, differs by provider: check each provider's token documentation rather than assuming.
What to test:
- A refresh after the access token expires, driven by a controllable clock.
- Two workers refreshing at once with the same refresh token: one should win, and the other must not leave the client holding a dead token.
- Replaying a refresh token that has already been rotated: expect
invalid_grant. - A refresh token revoked by the user or an admin, or expired by the provider's own policy:
invalid_grant, and your app should send the user back through sign-in rather than retry. - Requesting a wider scope on refresh than was originally granted:
invalid_scope.
Read more: Testing OAuth Token Refresh and Expiry Scenarios Locally · Replay Auth State Across Token Lifecycle Stages Without Hitting Production
Device authorization (device code)
The device asks the authorization server for a device_code, a short user_code and a verification URL. It shows the code and URL to the user, who finishes signing in on a phone or laptop. Meanwhile the device polls the token endpoint with the device code until the user approves, denies, or the code expires.
RFC 8628 defines the answers the device gets while it waits: authorization_pending (keep polling), slow_down (add 5 seconds to the polling interval from now on), access_denied and expired_token. When the server gives no interval, the client should wait 5 seconds between polls.
What to test:
- Several
authorization_pendingresponses followed by success. - A
slow_downresponse, checking that the client really lengthens its interval for every later poll. - The user denying the request (
access_denied) and the code timing out (expired_token), both of which should stop polling. - Polling with a device code that has already been exchanged for tokens.
JWT bearer assertion
Instead of sending a client secret, the client sends a JWT it has signed (or one issued to it by a trusted identity provider) in the assertion parameter. The authorization server checks the signature, issuer, audience and expiry, then issues an access token. Service accounts and workload identity federation, such as a CI runner's OIDC token being traded for cloud credentials, are built on this pattern.
RFC 7523 says a JWT that isn't valid, or whose signature can't be verified, is answered with invalid_grant.
What to test:
- An assertion signed with the wrong key, or with a key the server has rotated out.
- An
audthat doesn't match the token endpoint, and anissorsubthe server doesn't trust. - An expired assertion, and clock skew between the client and server.
- A trust policy scoped tightly enough that a token from another repository, branch or environment is refused.
Read more: Running OAuth Integration Tests in GitHub Actions Without Live Credentials
Token exchange
A service that already holds a token posts it as the subject_token and asks for a different one: a token for another audience, with less scope, or one that records which service is acting on the user's behalf (actor_token). It's the grant to reach for when an API gateway or an AI agent calls downstream services and shouldn't simply pass the user's original token along.
Support varies widely between identity providers, and so do the token types each one accepts, so check the provider's documentation before designing around it. RFC 8693 answers an invalid or unacceptable subject or actor token with invalid_request, and an audience or resource it won't issue for with invalid_target.
What to test:
- The exchanged token carries no more scope than the subject token did.
- An expired or revoked subject token can't be exchanged.
- A request for an audience the service isn't allowed to reach is refused.
Read more: AI Agent OAuth Scopes and Permission Boundaries for Tool-Calling
Implicit and password: the grants to retire
The implicit grant returned the access token directly in the redirect URL, where it could leak through browser history, referrer headers and logs, and it had no way to bind the token to the client that asked for it. RFC 9700 says clients should not use it; authorization code with PKCE replaces it, including in single-page apps.
The resource owner password credentials grant (grant_type=password) had the app collect the user's password and send it to the token endpoint, which rules out multi-factor sign-in and teaches users to type passwords into third-party apps. RFC 9700 says it must not be used. Both grants are left out of the OAuth 2.1 draft. If a test suite still depends on the password grant to get tokens quickly, replace it with a simulator that issues tokens through the real flow.
Token endpoint errors, and what invalid_grant means
RFC 6749 defines a short list of error codes the token endpoint returns in a JSON body with HTTP 400 (or 401 for some client authentication failures). Every grant above shares them, which is why a good test suite asserts on the error value, not only on the status code.
| error | What it means | Typical cause in tests |
|---|---|---|
invalid_request | A required parameter is missing, repeated or malformed | A test that builds the request body by hand and drops redirect_uri or code_verifier |
invalid_client | Client authentication failed | Wrong secret, wrong client ID for the environment, or secret sent in the body when the server expects Basic auth |
invalid_grant | The code, refresh token or other grant is invalid, expired, revoked, doesn't match the redirect URI, or was issued to another client | Reused or expired codes, rotated refresh tokens, PKCE mismatches, bad assertions |
unauthorized_client | This client isn't allowed to use this grant type | An app registration that doesn't have the grant enabled |
unsupported_grant_type | The server doesn't support this grant type | Calling a provider with password or a URN grant it never implemented |
invalid_scope | The scope is invalid, unknown, or wider than what was granted | Asking for more scope on refresh, or a scope name from another provider |
Read more: Fault Injection for Auth Endpoints in CI Pipelines · Token State Drift Between CI Runs and How to Lock Your Auth Simulator to a Known Starting Grant
Testing every grant without live credentials
Most of these failures depend on state: a code that was already redeemed, a refresh token that was already rotated, a device code that was already approved. A stateless mock that returns a canned token for every request can't produce any of them, so a suite built on one passes while the error paths go untested.
What works is a simulator that remembers what each grant issued, enforces the order the specification requires, lets tests move its clock, and starts every CI run from a known set of clients, users and tokens. Then each failure in this guide becomes one deliberate step in a test rather than something you wait for in production.
Read more: Wiring a Fully Controllable Auth Simulator Into Your Integration Test Suite for Stateful Grant Sequences · Stateful vs. Stateless Auth Mocking in Integration Test Suites
Sources
- RFC 6749: The OAuth 2.0 Authorization Framework
- RFC 7636: Proof Key for Code Exchange (PKCE)
- RFC 8628: OAuth 2.0 Device Authorization Grant
- RFC 7523: JWT Profile for OAuth 2.0 Client Authentication and Authorization Grants
- RFC 8693: OAuth 2.0 Token Exchange
- RFC 9700: Best Current Practice for OAuth 2.0 Security