Autentificarea cu cheie API folosind requests
Autentificarea bazată pe cheie API funcționează similar cu Autentificarea de bază, însă trebuie să incluzi o cheie API unică fie printr-un antet al cererii, fie printr-un parametru URL pentru cererile autentificate. Hai să explorăm ambele abordări.
Bine de știut:
- Pachetul
requestsa fost deja importat. - Folosește cheia API/token-ul
8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3pentru autentificare.
Acest exercițiu face parte din cursul
Introducere în API-uri cu Python
Exercițiu interactiv practic
Încearcă acest exercițiu completând acest cod de exemplu.
# 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')