OAuth 2.0 and Third-Party AuthLesson 4.1
OAuth 2.0 authorization code flow step by step
OAuth roles, authorization code flow, redirect URI, authorization code exchange, access token, refresh token, PKCE overview, state parameter
OAuth 2.0 Authorization Code Flow
OAuth 2.0 is an authorization framework, not an authentication protocol. It lets users grant your app access to their data on another service without sharing their password.
The authorization code flow — the only flow you should use for server-side apps:
- Authorization request: Redirect user to the provider's auth URL with your
client_id,redirect_uri,scope, and a randomstateparameter. - User grants permission: Provider authenticates the user and asks for consent.
- Authorization code: Provider redirects back to your
redirect_uriwith a short-livedcodeand thestateyou sent. - Verify state: Check that the
statematches what you sent — this prevents CSRF on the OAuth callback. - Token exchange: Your server POSTs to the provider's token endpoint with
code,client_id, andclient_secret. You receive an access token (and optionally a refresh token). - API calls: Use the access token to fetch user data from the provider's API.
The authorization code is single-use and short-lived (minutes). Tokens are only exchanged server-to-server — the client never sees the client secret or the access token exchange. This is why this flow is secure for server-side apps.
