0
0
Rest APIprogramming~10 mins

Bearer token authentication 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 add the Authorization header with a bearer token.

Rest API
headers = {"Authorization": "Bearer [1]"}
Drag options to blanks, or click blank then click option'
Amy_token_123
Bauth_token
C12345
Dtoken123
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to include the 'Bearer ' prefix.
Using the wrong token string.
2fill in blank
medium

Complete the code to send a GET request with bearer token authentication using the requests library.

Rest API
response = requests.get(url, headers=[1])
Drag options to blanks, or click blank then click option'
A{"Authorization": "token123"}
B{"Authorization": "Bearer token123"}
C{"Auth": "Bearer token123"}
D{"Authorization": "Bearer"}
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Auth' instead of 'Authorization' as the header key.
Omitting the 'Bearer ' prefix in the header value.
3fill in blank
hard

Fix the error in the code to correctly extract the bearer token from the Authorization header.

Rest API
token = request.headers.get('Authorization').[1](' ')[1]
Drag options to blanks, or click blank then click option'
Asplit
Bjoin
Creplace
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using join instead of split.
Trying to strip or replace without splitting.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that extracts tokens from a list of Authorization headers.

Rest API
tokens = {i: header[1](' ')[[2]] for i, header in enumerate(headers)}
Drag options to blanks, or click blank then click option'
Asplit
B1
C0
Dstrip
Attempts:
3 left
💡 Hint
Common Mistakes
Using index 0 which gives 'Bearer' instead of the token.
Using strip instead of split.
5fill in blank
hard

Fill all three blanks to create a function that validates a bearer token from headers.

Rest API
def validate_token(headers):
    auth = headers.get([1])
    if auth and auth.startswith([2]):
        return auth.split([3])[1]
    return None
Drag options to blanks, or click blank then click option'
A"Authorization"
B"Bearer "
C" "
D"Token"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Token' instead of 'Bearer ' as prefix.
Not including the space in the prefix.
Splitting by wrong character.