Authentification d’API

Introduction aux API en Python

Chris Ramakers

Engineering Manager

Accéder à des données sensibles

Un utilisateur tente d’accéder à des albums via un client. La requête est envoyée via Internet à un serveur par une API mais reçoit « 401 Unauthorized ».

Introduction aux API en Python

Accéder à des données sensibles

Un utilisateur accède à des albums via un client en fournissant un nom d’utilisateur et un mot de passe. La requête, envoyée via Internet à un serveur par une API, reçoit « 200 OK », indiquant un accès réussi.

Introduction aux API en Python

Méthodes d’authentification

Méthode Facilité de mise en œuvre Niveau de sécurité
Authentification Basic ⭐ ⭐ ⭐ ⭐ ⭐ ⭐ ☆ ☆ ☆ ☆
Clé/jeton d’API ⭐ ⭐ ⭐ ⭐ ☆ ⭐ ⭐ ☆ ☆ ☆
Authentification JWT ⭐ ⭐ ⭐ ☆ ☆ ⭐ ⭐ ⭐ ⭐ ☆
OAuth 2.0 ⭐ ⭐ ☆ ☆ ☆ ⭐ ⭐ ⭐ ⭐ ⭐

 

Astuce : consultez la documentation de l’API utilisée pour savoir quelle méthode d’authentification employer !

Introduction aux API en Python

Authentification Basic

Authentification Basic avec requests

# Ajoute automatiquement un en-tête Basic Auth avant l’envoi
requests.get('http://api.music-catalog.com', auth=('username', 'password'))
Introduction aux API en Python

Authentification par clé/jeton d’API

Via un paramètre de requête

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

Via l’en-tête d’autorisation « Bearer »

headers = {'Authorization': 'Bearer faaa1c97bd3f4bd9b024c708c979feca'}
requests.get('http://api.music-catalog.com/albums', headers=headers)
Introduction aux API en Python

Passons à la pratique !

Introduction aux API en Python

Preparing Video For Download...