Consider the following simplified steps of an Authorization Code Flow in a REST API:
- Client redirects user to authorization server with client_id and redirect_uri.
- User logs in and authorizes the client.
- Authorization server redirects back with a code.
- Client exchanges the code for an access token.
Given the code snippet below simulating the token exchange, what is the value of access_token after execution?
def exchange_code_for_token(code): if code == "auth123": return {"access_token": "token_abc123", "expires_in": 3600} else: return {"error": "invalid_code"} response = exchange_code_for_token("auth123") access_token = response.get("access_token", None) print(access_token)
Look at what the function returns when the code matches "auth123".
The function exchange_code_for_token returns a dictionary with the key access_token set to "token_abc123" when the code is "auth123". The variable access_token extracts this value, so the output is "token_abc123".
In the Authorization Code Flow, the client first redirects the user to the authorization server. The user logs in and the server redirects back with a code. The client then uses this code to request an access token.
Which important step is missing from this description?
Think about how the client proves it is allowed to exchange the code.
In the Authorization Code Flow, the client must authenticate itself (usually with client ID and secret) when exchanging the authorization code for an access token. This prevents misuse of the code by unauthorized parties.
Look at the HTTP POST request below that the client sends to exchange the authorization code for an access token:
POST /token HTTP/1.1 Host: auth.example.com Content-Type: application/x-www-form-urlencoded code=abc123&redirect_uri=https://client.app/callback
The server responds with an error: invalid_client.
What is the most likely reason for this error?
Think about how the server verifies the client identity during token exchange.
The invalid_client error means the server did not receive valid client credentials. The client must authenticate itself (usually with client_id and client_secret) when exchanging the code for a token.
A client receives a redirect URL after user authorization:
https://client.app/callback?code=xyz789&state=abc
Which Python code correctly extracts the code parameter value?
Use standard Python libraries to parse URLs safely.
Option A uses urlparse and parse_qs to safely extract query parameters. Other options rely on fragile string splitting that can break if order changes.
Order the following steps correctly as they happen in the Authorization Code Flow:
Think about the natural order of user interaction and server responses.
The correct order is: client sends user to authorization server (1), user logs in and authorizes (2), server redirects back with code (3), client exchanges code for token (4).