Authentification API

Introduction aux API en Python

Chris Ramakers

Engineering Manager

Accès aux données sensibles

Un utilisateur essayant d’accéder à des albums de musique via un client. La requête est envoyée sur Internet à un serveur via une API, mais reçoit une réponse « 401 Unauthorized ».

Introduction aux API en Python

Accès aux données sensibles

Un utilisateur accédant à des albums de musique via un client, en fournissant un nom d’utilisateur et un mot de passe. La requête est envoyée sur Internet à un serveur via une API et reçoit une réponse « 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 de base ⭐ ⭐ ⭐ ⭐ ⭐ ⭐ ☆ ☆ ☆ ☆
Authentification par clé/API token ⭐ ⭐ ⭐ ⭐ ☆ ⭐ ⭐ ☆ ☆ ☆
Authentification JWT ⭐ ⭐ ⭐ ☆ ☆ ⭐ ⭐ ⭐ ⭐ ☆
OAuth 2.0 ⭐ ⭐ ☆ ☆ ☆ ⭐ ⭐ ⭐ ⭐ ⭐

Conseil : Consultez la documentation de l’API utilisée pour connaître méthode authentification !

Introduction aux API en Python

Authentification de base

Authentification de base avec le package requests

# This will automatically add a Basic Authentication header before sending the request
requests.get('http://api.music-catalog.com', auth=('username', 'password'))
Introduction aux API en Python

Authentification par clé API/token

Utilisation paramètre 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)

Utilisation en-tête 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...