Authenticatie met API-sleutel met requests
Authenticatie op basis van een API-sleutel werkt vergelijkbaar met Basic Authentication, maar je moet een unieke API-sleutel toevoegen via een requestheader of een URL-parameter voor geauthenticeerde requests. Laten we beide aanpakken verkennen.
Goed om te weten:
- Het pakket
requestsis al geïmporteerd. - Gebruik de API-sleutel/token
8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3om te authenticeren.
Deze oefening maakt deel uit van de cursus
Introductie tot API's 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')