Implement OAuth 2.0 flow to get an access token for API requests
Tests your grasp of OAuth 2.0 grant-type selection and token lifecycle. Strong answers match the script context to client credentials or authorization code flow, detail the token endpoint exchange, and address refresh and expiry.
What's really being asked
Whether you can select the correct OAuth 2.0 grant type for a scripted, potentially non-interactive client and explain the full token lifecycle from issuance to usage to refresh. Senior engineers are expected to understand the security boundaries between the authorization endpoint, token endpoint, and resource server, not just parrot that you add Bearer to a header.
The full answer
First, grant-type selection. For a server-side script acting on its own behalf, client credentials is usually correct. For a script acting on behalf of a user without a browser, authorization code with a stored refresh token is the alternative. Second, the authorization phase. In client credentials, the script authenticates directly to the token endpoint using its client_id and client_secret. In authorization code, the script or a one-time setup redirects a user to the authorization endpoint with response_type=code, scope, state, and redirect_uri, then captures the authorization code. Third, token issuance. The script POSTs to the token endpoint with grant_type, the code or credentials, and optional scope to receive an access_token, token_type, expires_in, and optionally a refresh_token. Fourth, making requests. The script attaches the access token to API requests in the Authorization header using the token_type, typically as Bearer. Fifth, lifecycle management. The script must store tokens securely, not log them, handle 401 responses by using the refresh_token to obtain a new access token, and gracefully handle failures by stopping rather than looping infinitely.
The mistakes people make
Treating OAuth 2.0 like a static API key that never expires. Describing the implicit flow for a backend script, which is insecure and largely deprecated. Omitting client authentication at the token endpoint. Storing client secrets or refresh tokens in source control. Failing to mention TLS for all token transmissions. Suggesting the resource owner password credentials grant as a first choice, which RFC 6749 explicitly discourages.
What usually comes next
How would you handle token storage securely in a CI environment? What happens if the refresh token is revoked or expires? How do you choose between client credentials and authorization code for a daemon that acts on behalf of multiple users? How would you implement PKCE if this were a native script without a secret?
A concrete example
A Python ETL script needs nightly Salesforce data. It uses the client credentials grant. It stores CLIENT_ID and CLIENT_SECRET in environment variables. It POSTs to the token endpoint with grant_type=client_credentials. It receives an access token with a 7200-second expiry and no refresh token. It caches the token in memory, attaches Authorization: Bearer TOKEN to every API call, and when a call returns 401, it repeats the token request once, backs off on failure, and alerts rather than retrying forever.
Interview question
A Python daemon runs nightly without user interaction to pull API data on its own behalf. Which OAuth 2.0 approach should it use to obtain an access token?
- a.Use the resource owner password credentials grant, exchanging a service account username and password at the token endpoint.
- b.Use the client credentials grant, authenticating directly to the token endpoint with its client_id and client_secret.Correct
- c.Use the authorization code grant, redirecting a user to the authorization endpoint and storing a refresh token for later runs.
- d.Use the implicit flow, receiving an access token directly from the authorization endpoint via browser redirect.
Why? this is the answer
A daemon acting on its own behalf should use the client credentials grant and authenticate directly to the token endpoint with its client_id and client_secret. The authorization code flow with a stored refresh token is designed for scripts acting on behalf of a user, not for non-interactive service-to-service calls.
Just read this? Test yourself on what you have been reading.
Read the original → rfc-editor.org
- #oauth2
- #api
- #authentication
- #security
- #token-management
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles