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.

OAuth 2.0 grant types at a glance
Grantgrant_type valueUse it forStatus
Authorization code + PKCEauthorization_codeA user signing in through a browser: web apps, single-page apps, mobile and desktop appsCurrent default (RFC 6749, RFC 7636)
Client credentialsclient_credentialsA service calling an API as itself, with no user involvedCurrent (RFC 6749)
Refresh tokenrefresh_tokenGetting a new access token after the old one expires, without asking the user againCurrent (RFC 6749)
Device authorizationurn:ietf:params:oauth:grant-type:device_codeTVs, CLIs and other devices that can't open a browser or take typed input easilyCurrent (RFC 8628)
JWT bearer assertionurn:ietf:params:oauth:grant-type:jwt-bearerA workload presenting a signed JWT instead of a stored secretCurrent (RFC 7523)
Token exchangeurn:ietf:params:oauth:grant-type:token-exchangeA service trading one token for another to call a downstream API on a user's behalfCurrent (RFC 8693)
ImplicitNone (token returned from the authorization endpoint)Older browser appsDeprecated: should not be used (RFC 9700)
Resource owner passwordpasswordApps that collected the user's password directlyDeprecated: 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 a redirect_uri that differs from the one in the authorization request: both are invalid_grant.
  • A mismatched or missing state on 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_pending responses followed by success.
  • A slow_down response, 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 aud that doesn't match the token endpoint, and an iss or sub the 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.

Standard token endpoint errors (RFC 6749, section 5.2)
errorWhat it meansTypical cause in tests
invalid_requestA required parameter is missing, repeated or malformedA test that builds the request body by hand and drops redirect_uri or code_verifier
invalid_clientClient authentication failedWrong secret, wrong client ID for the environment, or secret sent in the body when the server expects Basic auth
invalid_grantThe code, refresh token or other grant is invalid, expired, revoked, doesn't match the redirect URI, or was issued to another clientReused or expired codes, rotated refresh tokens, PKCE mismatches, bad assertions
unauthorized_clientThis client isn't allowed to use this grant typeAn app registration that doesn't have the grant enabled
unsupported_grant_typeThe server doesn't support this grant typeCalling a provider with password or a URN grant it never implemented
invalid_scopeThe scope is invalid, unknown, or wider than what was grantedAsking 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

  1. RFC 6749: The OAuth 2.0 Authorization Framework
  2. RFC 7636: Proof Key for Code Exchange (PKCE)
  3. RFC 8628: OAuth 2.0 Device Authorization Grant
  4. RFC 7523: JWT Profile for OAuth 2.0 Client Authentication and Authorization Grants
  5. RFC 8693: OAuth 2.0 Token Exchange
  6. RFC 9700: Best Current Practice for OAuth 2.0 Security