GeekCoding101 make your way to geek

OAuth 2.0 Grant Types: Which to Use, and Is PKCE One?

OAuth 2.0 grant types compared: authorization code with PKCE, client credentials, device code, refresh token, and why implicit and password are retired.

8 min read
Oauth 2.0 Grant Types
On this pageList of Grant Types

An OAuth 2.0 grant type is the method a client uses to get an access token from an authorization server. Today you only need four of them:

  • Authorization code with PKCE when a user signs in.
  • Client credentials for machine-to-machine calls.
  • Device code for TVs, CLIs and other devices without a usable browser.
  • Refresh token to keep a session alive.

The other two from the original spec, implicit and password, are legacy, and you should plan to migrate away from them.

This post covers four things:

  • The full list of grant types.
  • What each one looks like on the wire.
  • Which one to pick for your app.
  • Why PKCE is not a grant type, even though you’ll use it almost everywhere.

List of Grant Types

Grant typegrant_type value at the token endpointUsed forStatus
Authorization Code (+ PKCE)authorization_codeA user signs in and the app acts on their behalf: web apps, SPAs, mobile and native appsRecommended, with PKCE for every client type
Client Credentialsclient_credentialsBackend services, jobs and pipelines calling an API as themselves, with no userRecommended for machine-to-machine
Device Authorization (Device Code)urn:ietf:params:oauth:grant-type:device_codeSmart TVs, consoles, CLI tools, IoTRecommended when the device has no browser or keyboard
Refresh Tokenrefresh_tokenGetting a new access token without sending the user through login againRecommended alongside one of the above
Implicitnone (token comes back from /authorize with response_type=token)Old browser-based JavaScript appsDeprecated; removed in OAuth 2.1
Resource Owner Password CredentialspasswordApps that collect the user’s username and password directlyNot recommended; removed in OAuth 2.1

Two notes on the table:

  • Grants defined by the specs. RFC 6749 defines four grants: authorization code, implicit, password and client credentials. It also defines how refresh tokens are exchanged, plus an extension mechanism. Device code is one of those extensions (RFC 8628), which is why its grant_type is a URN.
  • Default choice. If you’re unsure, start with authorization code with PKCE. It covers the largest set of cases and is what current security guidance points to.

How Each Grant Type Works

The table tells you which grant to pick. This section shows what each one actually does on the wire. Parameter names follow RFC 6749, RFC 7636 for PKCE, and RFC 8628 for the device flow. Example values are taken from the RFCs.

Authorization Code with PKCE

Use it when a user signs in and your app acts on their behalf. That covers server-rendered web apps, single-page apps, and native or mobile apps. The flow has four steps:

  1. The client sends the user to the authorization server.
  2. The user logs in and consents.
  3. The server redirects back with a short-lived code.
  4. The client exchanges that code for tokens over a direct back-channel request.

With PKCE, the client first creates a random code_verifier. It sends the verifier’s SHA-256 hash as code_challenge in the authorization request:

GET /authorize?response_type=code
&client_id=my-app
&redirect_uri=https://app.example.com/callback
&scope=openid%20profile
&state=af0ifjsldkj
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
&code_challenge_method=S256

When it redeems the code, the client sends the original verifier in the token request:

POST /token
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code
&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https://app.example.com/callback
&client_id=my-app
&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

An attacker who steals the code from the redirect cannot redeem it, because they do not have the verifier. Current guidance treats PKCE as follows:

  • Public clients: PKCE is required by the OAuth 2.0 Security Best Current Practice (RFC 9700) and the OAuth 2.1 draft.
  • Confidential clients: PKCE is recommended too.

For every redirect and request in order, see OAuth 2.0 Authorization Code Flow, Step by Step.

Client Credentials

Use it for machine-to-machine calls where there is no user, such as a backend service, a cron job or a CI pipeline calling an API as itself. The client authenticates with its own credentials and gets an access token directly. There is no browser and no redirect:

POST /token
Authorization: Basic base64(client_id:client_secret)
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&scope=reports.read

Because there is no user, the token represents the application itself. Keep the scopes you grant as narrow as possible.

Only confidential clients, the ones that can actually keep a secret, should use this grant. Never ship a client secret inside a browser or mobile app.

Device Authorization (Device Code)

Use it for devices that have no browser or an awkward keyboard: smart TVs, game consoles, CLI tools and IoT. The flow works like this:

  1. The device asks the authorization server for a device code.
  2. The device shows the user a short user_code and a URL.
  3. The user completes the login on their phone or laptop.
  4. Meanwhile, the device polls the token endpoint.
POST /device_authorization
Content-Type: application/x-www-form-urlencoded
client_id=my-tv-app&scope=profile
{
"device_code": "GmRhmhcxhwAzkoEqiMEg_DnyEysNkuNhszIySk9eS",
"user_code": "WDJB-MJHT",
"verification_uri": "https://example.com/device",
"interval": 5
}
POST /token
Content-Type: application/x-www-form-urlencoded
grant_type=urn:ietf:params:oauth:grant-type:device_code
&device_code=GmRhmhcxhwAzkoEqiMEg_DnyEysNkuNhszIySk9eS
&client_id=my-tv-app

How the server answers each poll:

  • User hasn’t finished: the server returns authorization_pending. Keep polling at the given interval.
  • Polling too fast: the server returns slow_down. Back off.
  • User approved: the response contains the tokens.

Device codes are short-lived. The user code should be hard to guess, because anyone who types it in can approve the request.

Refresh Token

A refresh token lets a client get a new access token without sending the user through login again. The two tokens play different roles:

  • Access token: kept short-lived, so a leak has limited impact.
  • Refresh token: longer-lived, kept on the server or in secure storage, and exchanged at the token endpoint with its own grant type.
POST /token
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token
&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
&client_id=my-app

The server returns a new access token and often a new refresh token as well.

For public clients, current guidance is to use refresh token rotation:

  • Each refresh token works only once.
  • If an old one is replayed, the server revokes the whole token family.

If the app needs to keep working after the user has left, request the offline_access scope, or whatever your provider uses instead.

Refresh token is not something you choose instead of the other grants. You use it with them: it extends a session that started with authorization code or device code.

Implicit returned the access token directly in the URL fragment of the redirect. That caused two problems:

  • Tokens ended up in browser history, referrer headers and logs.
  • There was no way to bind a token to the client that asked for it.

RFC 9700 advises against implicit, and the OAuth 2.1 draft drops it. Single-page apps should use authorization code with PKCE instead.

Resource Owner Password Credentials has the app collect the user’s username and password and send them to the token endpoint. This causes several problems:

  • It teaches users to type their password into third-party apps.
  • It breaks multi-factor authentication and single sign-on.
  • It gives the client far more power than it needs.

The OAuth 2.1 draft removes it as well. Migrate to authorization code with PKCE, or to the device flow when there is no browser.

Which Grant Type Should I Use?

Your appUse
Web app with a server backend, user signs inAuthorization Code (with PKCE)
Single-page app or mobile/native appAuthorization Code with PKCE
Backend service or script, no userClient Credentials
TV, console, CLI or IoT deviceDevice Code
Keeping a session alive without re-loginRefresh Token alongside one of the above
Old app still on Implicit or PasswordPlan a migration to the rows above

Once a grant gives you tokens, ID token vs access token explains which one to send to your API. If you are protecting an MCP server with these flows, see MCP authorization with OAuth 2.1.

Is PKCE A Grant Type?

No. PKCE (Proof Key for Code Exchange, RFC 7636) is not a grant type in OAuth 2.0.

PKCE is an extension to the authorization code flow. When you use it, the grant type is still authorization_code. PKCE only adds two parameters:

  • code_challenge on the authorization request.
  • code_verifier on the token request.

It was designed for clients that cannot reliably store a client secret, such as mobile, native and browser apps. It does this with a dynamically generated secret (the code verifier) and a hash-based challenge (the code challenge). This mitigates authorization code interception: a stolen code is useless without the verifier.

Because it is cheap and closes a real attack, current guidance recommends PKCE for confidential clients too, not only public ones.

FAQ

How many OAuth 2.0 grant types are there? The core spec, RFC 6749, defines four grants (authorization code, implicit, resource owner password credentials and client credentials) plus the refresh token exchange. It also allows extension grants; device code (RFC 8628) is the one you are most likely to use. In practice, a modern app needs authorization code with PKCE, client credentials, device code and refresh token.

Is refresh token a grant type? At the token endpoint, yes: you send grant_type=refresh_token. Unlike the others, though, it can’t start a session. It only renews access that was first granted through another flow, such as authorization code or device code.

What’s the difference between authorization code and implicit? With authorization code, the redirect carries only a short-lived code, and the tokens come back over a direct back-channel request. With PKCE, that code is bound to the client that started the flow. Implicit put the access token itself in the redirect URL, where it could leak. That is why implicit is deprecated and SPAs now use authorization code with PKCE.

Should a confidential client still use PKCE? Yes. Confidential clients can authenticate with a secret. Even so, PKCE adds protection against code interception and injection at almost no cost, and current guidance recommends it for all clients.

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.

Keep reading

All posts