API key authentication with requests
API key-based authentication functions similarly to Basic Authentication, but you must include a unique API key using either a request header or a URL parameter for authenticated requests. Let's explore both approaches.
Good to know:
- The
requests
package has already been imported. - Use the API key/token
8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3
to authenticate.
This exercise is part of the course
Introduction to APIs in Python
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a dictionary containing the API key using the correct key-value combination
params = {____: ____}
# Add the dictionary to the requests.get() call using the correct function argument
response = requests.get('http://localhost:3000/albums', ____=params)
if(response.status_code == 200):
print("Success!")
elif(response.status_code == 401):
print('Authentication failed')
else:
print('Another error occurred')