OAuth 2.0 Authorization Code Flow with PKCE, Explained Step by Step

How OAuth 2.0 delegated authorization works: roles, the authorization code flow with PKCE, scopes, access and refresh tokens, and common security mistakes.

Published December 4, 2026

OAuth 2.0 lets a user grant an application limited access to their data on another service without sharing their password: "allow this calendar app to read my Google Calendar." The authorization code flow with PKCE is the recommended flow for almost every client today, including single-page apps and mobile apps. Understanding it step by step removes most of the mystery from login integrations.

The concept

OAuth has four roles: the resource owner (the user), the client (your app), the authorization server (which authenticates the user and issues tokens) and the resource server (the API holding the data). The client redirects the user to the authorization server with the scopes it wants; the user logs in and consents; the authorization server redirects back with a short-lived authorization code; the client exchanges the code for tokens.

PKCE (Proof Key for Code Exchange) protects that exchange. The client creates a random code_verifier, sends its SHA-256 hash (code_challenge) in the initial request, and later proves possession by sending the original verifier when redeeming the code. An attacker who intercepts the code cannot redeem it. OAuth 2.1 consolidates best practices: PKCE everywhere, no implicit flow, no password grant, exact redirect URI matching.

The flow

UserClient appAuth serverAPIredirect: /authorize?code_challenge=…&scope=…log in + consentredirect back with ?code=…&state=…POST /token code + code_verifieraccess_token (+ refresh_token)GET /events Authorization: Bearer …
Authorization code flow with PKCE.

Tokens involved

TokenPurposeLifetimeStore
Authorization codeOne-time exchange for tokensSeconds to minutesNever stored
Access tokenCall APIsMinutes to an hourMemory / server session
Refresh tokenGet new access tokensDays to months, rotatedServer-side or secure storage
ID token (OIDC)Who the user isShortValidate, then discard or keep claims

Common mistakes

MistakeFix
No state parameterRandom state, verified on callback (CSRF protection)
Loose redirect URI matchingExact, pre-registered redirect URIs
Implicit flow in SPAsAuthorization code + PKCE
Requesting broad scopesRequest minimum scopes, incrementally
Long-lived tokens in localStorageBackend-for-frontend or short-lived tokens in memory

Practical examples

Create PKCE values

Verifier is random; challenge is its SHA-256, base64url-encoded.

import secrets, hashlib, base64
verifier = base64.urlsafe_b64encode(secrets.token_bytes(32)).rstrip(b"=").decode()
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode()

Authorization request

Redirect the browser to the authorization server.

GET https://auth.example.com/authorize?
    response_type=code
    &client_id=calendar-app
    &redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
    &scope=openid%20calendar.read
    &state=9f3a1c...
    &code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
    &code_challenge_method=S256

Token exchange

Confidential clients also authenticate; public clients rely on PKCE.

POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=SplxlOBe...&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&client_id=calendar-app&code_verifier=dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk

Common mistakes

  • Using OAuth access tokens as proof of identity (use OpenID Connect ID tokens).
  • Skipping state or PKCE validation.
  • Wildcard or partially matched redirect URIs.
  • Storing refresh tokens in browser localStorage.

Key takeaways

  • OAuth 2.0 delegates limited access without sharing passwords.
  • Authorization code + PKCE is the default flow for all clients.
  • Validate state, use exact redirect URIs, request minimal scopes.
  • Keep tokens short-lived; protect and rotate refresh tokens.

FAQ

Is OAuth authentication?

OAuth is authorization. OpenID Connect adds authentication on top with ID tokens and a userinfo endpoint.

Do server-side web apps need PKCE?

Yes, current best practice (and OAuth 2.1) recommends PKCE for all clients, including confidential ones.

What is the client credentials flow?

A flow for machine-to-machine access with no user involved; the client authenticates with its own credentials.

More Security articles