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
requestspackage has already been imported. - Use the API key/token
8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3to authenticate.
Deze oefening maakt deel uit van de cursus
Introduction to APIs in Python
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
# 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')