Aan de slagGa gratis aan de slag

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:

  1. The requests package has already been imported.
  2. Use the API key/token 8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3 to authenticate.

Deze oefening maakt deel uit van de cursus

Introduction to APIs in Python

Cursus bekijken

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')
Code bewerken en uitvoeren