0
0
Rest APIprogramming~10 mins

Token refresh mechanism in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the refresh token from the request headers.

Rest API
refresh_token = request.headers.get([1])
Drag options to blanks, or click blank then click option'
A"Content-Type"
B"Authorization"
C"Refresh-Token"
D"User-Agent"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Authorization' header instead of 'Refresh-Token'.
Trying to get the token from 'Content-Type' header.
2fill in blank
medium

Complete the code to verify if the refresh token is valid before issuing a new access token.

Rest API
if not verify_token([1]):
    return {'error': 'Invalid refresh token'}, 401
Drag options to blanks, or click blank then click option'
Arefresh_token
Baccess_token
Cuser_id
Drequest_token
Attempts:
3 left
💡 Hint
Common Mistakes
Verifying the access token instead of the refresh token.
Using an undefined variable like 'request_token'.
3fill in blank
hard

Fix the error in the code that generates a new access token using the user ID from the refresh token payload.

Rest API
user_id = decode_token([1])['user_id']
new_access_token = generate_access_token(user_id)
Drag options to blanks, or click blank then click option'
Apayload
Brefresh_token
Crequest.headers
Daccess_token
Attempts:
3 left
💡 Hint
Common Mistakes
Decoding the access token instead of the refresh token.
Trying to decode the entire request headers.
4fill in blank
hard

Fill both blanks to create a JSON response with the new access token.

Rest API
return { [1]: [2] }
Drag options to blanks, or click blank then click option'
A"access_token"
B"message": "Token refreshed successfully"}
Cnew_access_token
D"access_token": new_access_token
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a string instead of a dictionary.
Using incorrect keys or missing braces.
5fill in blank
hard

Fill all three blanks to implement a complete token refresh endpoint that extracts, verifies, decodes, and returns a new access token.

Rest API
def refresh_token_endpoint(request):
    [1] = request.headers.get("Refresh-Token")
    if not verify_token([2]):
        return {'error': 'Invalid refresh token'}, 401
    user_id = decode_token([3])['user_id']
    new_access_token = generate_access_token(user_id)
    return {'access_token': new_access_token}
Drag options to blanks, or click blank then click option'
Arefresh_token
Daccess_token
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for the refresh token in different places.
Confusing access token with refresh token.