API-authenticatie

Introductie tot API's in Python

Chris Ramakers

Engineering Manager

Toegang tot gevoelige data

Een gebruiker probeert via een client muziek-albums te openen. Het verzoek gaat via internet naar een server via een API maar krijgt "401 Unauthorized" terug.

Introductie tot API's in Python

Toegang tot gevoelige data

Een gebruiker opent muziek-albums via een client met gebruikersnaam en wachtwoord. Het verzoek gaat via internet naar een server via een API en krijgt "200 OK" terug: toegang gelukt.

Introductie tot API's in Python

Authenticatiemethoden

Methode Implementatiegemak Beveiliging
Basic authentication ⭐ ⭐ ⭐ ⭐ ⭐ ⭐ ☆ ☆ ☆ ☆
API-sleutel/-token ⭐ ⭐ ⭐ ⭐ ☆ ⭐ ⭐ ☆ ☆ ☆
JWT-authenticatie ⭐ ⭐ ⭐ ☆ ☆ ⭐ ⭐ ⭐ ⭐ ☆
OAuth 2.0 ⭐ ⭐ ☆ ☆ ☆ ⭐ ⭐ ⭐ ⭐ ⭐

 

Tip: check de API-documentatie om te zien welke authenticatiemethode je moet gebruiken!

Introductie tot API's in Python

Basic authentication

Basic authentication met het requests-pakket

# Voegt automatisch een Basic Authentication-header toe vóór het verzenden
requests.get('http://api.music-catalog.com', auth=('username', 'password'))
Introductie tot API's in Python

API-sleutel/-token-authenticatie

Via een queryparameter

http://api.music-catalog.com/albums?access_token=faaa1c97bd3f4bd9b024c708c979feca
params = {'access_token': 'faaa1c97bd3f4bd9b024c708c979feca'}
requests.get('http://api.music-catalog.com/albums', params=params)

Met de "Bearer" Authorization-header

headers = {'Authorization': 'Bearer faaa1c97bd3f4bd9b024c708c979feca'}
requests.get('http://api.music-catalog.com/albums', headers=headers)
Introductie tot API's in Python

Laten we oefenen!

Introductie tot API's in Python

Preparing Video For Download...