What Is OAuth? ('Login with Google' Explained)
OAuth is how apps get permission to act on your behalf โ without you handing over your password. Here's the idea.
OAuth 2.0 is the standard way for an application to get permission to access a user’s data on another service — without the user handing over their password. When you click ‘Sign in with Google’ or let an app ‘connect to your GitHub’, that is OAuth.
The players
| Resource owner | The user who owns the data. |
| Client | Your application, which wants access. |
| Authorization server | Where the user logs in and approves (e.g. accounts.google.com). |
| Resource server | The API holding the data (e.g. the Google Calendar API). |
The Authorization Code flow (the common one)
- 1. Your app redirects the user to the authorization server with your
client_id, thescopes you want, and aredirect_uri. - 2. The user logs in and approves the requested permissions.
- 3. The authorization server redirects back to your
redirect_uriwith a short-lived authorization code. - 4. Your backend exchanges that code (plus your
client_secret) for an access token and usually a refresh token. - 5. Your app calls the API with
Authorization: Bearer <access_token>. - 6. When the access token expires (often in an hour), use the refresh token to get a new one silently.
Public clients (single-page apps, mobile) add PKCE, a proof step that removes the need to ship a client secret.
Other grant types
| Client Credentials | No user involved — your app authenticates as itself for machine-to-machine access. Common for ‘app-only’ API tiers (e.g. Spotify catalogue search). |
| Device Code | For TVs and CLIs: the device shows a code, the user enters it on their phone. |
| Refresh Token | Exchanges a refresh token for a fresh access token. |
| Password (deprecated) | User types their password into your app. Avoid — it defeats the point of OAuth. |
Scopes
A scope is a named permission: read:user, repo, calendar.events.readonly. Request the minimum you need — users are more likely to approve, and a leaked token does less damage.
OAuth vs. OpenID Connect vs. API keys
- OAuth 2.0 is about authorization — what your app may do.
- OpenID Connect (OIDC) is a thin layer on top for authentication — proving who the user is, via an
id_token(a JWT). ‘Sign in with…’ buttons use OIDC. - API keys identify an application, not a user, and involve no consent screen. Simpler, but can’t represent ‘this specific user allowed this’.
Security essentials
- Do the code-for-token exchange on your server; never expose the
client_secret. - Validate the
stateparameter to prevent CSRF on the callback. - Store tokens encrypted; treat refresh tokens like passwords.
- Use exact
redirect_urimatching — open redirects are a classic OAuth attack.
FAQ
What is the difference between OAuth and an API key?
An API key identifies your application and is used for machine-to-machine calls. OAuth lets a user grant your app scoped, revocable access to their data on another service, without sharing their password.
What is a bearer token?
The access token OAuth issues. ‘Bearer’ means whoever holds it can use it — so it must be sent over HTTPS and kept secret. You send it as Authorization: Bearer <token>.
Why do access tokens expire so quickly?
To limit the damage if one leaks. Your app uses the longer-lived refresh token to get new access tokens without bothering the user again.
Is ‘Sign in with Google’ the same as OAuth?
It uses OAuth 2.0 plus OpenID Connect. OAuth handles the permission grant; OIDC adds an identity token that tells your app who the user is.
Keep going
Try it on a real API
๐ Explore free public APIs
Hundreds of free APIs, searchable by category, with auth & CORS at a glance.
Browse the directory โ