Bird
Raised Fist0
Rest APIprogramming~20 mins

Authorization code flow in Rest API - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Authorization Code Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the final access token value?

Consider the following simplified steps of an Authorization Code Flow in a REST API:

  1. Client redirects user to authorization server with client_id and redirect_uri.
  2. User logs in and authorizes the client.
  3. Authorization server redirects back with a code.
  4. 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?

Rest API
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)
ANone
Binvalid_code
Ctoken_abc123
Dauth123
Attempts:
2 left
💡 Hint

Look at what the function returns when the code matches "auth123".

🧠 Conceptual
intermediate
1:30remaining
Which step is missing in this Authorization Code Flow?

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?

AUser enters their password twice
BAuthorization server sends refresh token before access token
CClient directly receives the access token without a code
DClient authenticates itself when exchanging the code for the token
Attempts:
2 left
💡 Hint

Think about how the client proves it is allowed to exchange the code.

🔧 Debug
advanced
2:00remaining
Why does this token exchange request fail?

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?

AThe client did not include its client_id and client_secret in the request
BThe redirect_uri does not match the one used in the authorization request
CThe authorization code is expired
DThe Content-Type header is incorrect
Attempts:
2 left
💡 Hint

Think about how the server verifies the client identity during token exchange.

📝 Syntax
advanced
2:30remaining
Which code snippet correctly extracts the authorization code from a redirect URL?

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?

A
from urllib.parse import urlparse, parse_qs
url = 'https://client.app/callback?code=xyz789&state=abc'
query = urlparse(url).query
code = parse_qs(query)['code'][0]
print(code)
B
url = 'https://client.app/callback?code=xyz789&state=abc'
code = url.split('code=')[1].split('&')[0]
print(code)
C
url = 'https://client.app/callback?code=xyz789&state=abc'
code = url.split('?')[1].split('=')[1]
print(code)
D
url = 'https://client.app/callback?code=xyz789&state=abc'
code = url.split('&')[0].split('=')[1]
print(code)
Attempts:
2 left
💡 Hint

Use standard Python libraries to parse URLs safely.

🚀 Application
expert
3:00remaining
What is the correct sequence of steps in the Authorization Code Flow?

Order the following steps correctly as they happen in the Authorization Code Flow:

A2,1,3,4
B1,2,3,4
C1,3,2,4
D3,2,1,4
Attempts:
2 left
💡 Hint

Think about the natural order of user interaction and server responses.

Practice

(1/5)
1. What is the main purpose of the authorization code in the Authorization Code Flow?
easy
A. To exchange it for an access token securely
B. To directly access user data
C. To authenticate the user with a password
D. To refresh the access token automatically

Solution

  1. Step 1: Understand the role of the authorization code

    The authorization code is a temporary code given after user consent, not the token itself.
  2. Step 2: Identify what the app does with the code

    The app sends this code to the authorization server to get an access token securely.
  3. Final Answer:

    To exchange it for an access token securely -> Option A
  4. Quick Check:

    Authorization code = temporary code for token exchange [OK]
Hint: Authorization code is a temporary code, not a token [OK]
Common Mistakes:
  • Thinking the code directly accesses data
  • Confusing code with user password
  • Assuming code refreshes tokens
2. Which HTTP method is typically used by the app to exchange the authorization code for an access token?
easy
A. DELETE
B. GET
C. PUT
D. POST

Solution

  1. Step 1: Recall the token exchange request

    The app sends the authorization code to the token endpoint to get an access token.
  2. Step 2: Identify the HTTP method used

    This request uses POST because it sends data securely in the request body.
  3. Final Answer:

    POST -> Option D
  4. Quick Check:

    Token exchange uses POST method [OK]
Hint: Token exchange sends data securely, so use POST [OK]
Common Mistakes:
  • Using GET which exposes data in URL
  • Confusing PUT or DELETE with token exchange
  • Assuming token exchange is a simple GET request
3. Given this simplified token exchange request in Python:
import requests
response = requests.post('https://auth.example.com/token', data={
    'code': 'abc123',
    'client_id': 'myapp',
    'client_secret': 'secret',
    'redirect_uri': 'https://myapp.com/callback',
    'grant_type': 'authorization_code'
})
print(response.json().get('access_token'))
What will this code print if the exchange is successful?
medium
A. The authorization code 'abc123'
B. The access token string from the server
C. An error message about invalid client
D. None

Solution

  1. Step 1: Understand the request purpose

    The code sends a POST request to exchange the authorization code for an access token.
  2. Step 2: Analyze the printed output

    If successful, the server returns JSON with an 'access_token' key, which is printed.
  3. Final Answer:

    The access token string from the server -> Option B
  4. Quick Check:

    response.json()['access_token'] = access token [OK]
Hint: Successful exchange returns access token, not code [OK]
Common Mistakes:
  • Printing the code instead of token
  • Expecting error message on success
  • Not accessing JSON correctly
4. In the Authorization Code Flow, a developer wrote this code snippet to exchange the code:
response = requests.get('https://auth.example.com/token', params={
    'code': 'abc123',
    'client_id': 'myapp',
    'client_secret': 'secret',
    'redirect_uri': 'https://myapp.com/callback',
    'grant_type': 'authorization_code'
})
What is the main issue with this code?
medium
A. Incorrect redirect URI format
B. Missing the authorization code parameter
C. Using GET instead of POST for token exchange
D. Client secret should not be sent

Solution

  1. Step 1: Check HTTP method for token exchange

    The token exchange requires a POST request to send sensitive data securely.
  2. Step 2: Identify the problem in the code

    The code uses GET with query parameters, which is insecure and not standard for this flow.
  3. Final Answer:

    Using GET instead of POST for token exchange -> Option C
  4. Quick Check:

    Token exchange must use POST, not GET [OK]
Hint: Token exchange always uses POST, not GET [OK]
Common Mistakes:
  • Using GET exposes secrets in URL
  • Forgetting to send client secret
  • Assuming redirect URI format is wrong
5. A web app uses Authorization Code Flow with PKCE (Proof Key for Code Exchange). Which additional step does PKCE add to improve security?
hard
A. The app sends a code verifier with the token request to prove it initiated the flow
B. The app uses client secret only without authorization code
C. The user enters their password twice during login
D. The app skips the authorization code and uses implicit flow

Solution

  1. Step 1: Understand PKCE purpose

    PKCE adds a code verifier and challenge to prevent interception of the authorization code.
  2. Step 2: Identify the added step in the flow

    The app sends the code verifier with the token request to prove it started the flow and prevent attacks.
  3. Final Answer:

    The app sends a code verifier with the token request to prove it initiated the flow -> Option A
  4. Quick Check:

    PKCE adds code verifier step for security [OK]
Hint: PKCE adds code verifier to token request for security [OK]
Common Mistakes:
  • Thinking PKCE removes authorization code
  • Confusing PKCE with password prompts
  • Assuming PKCE uses implicit flow