requestsを使ったAPIキー認証
APIキーによる認証はBasic認証と似た仕組みですが、リクエストを認証するには、リクエストヘッダーまたはURLパラメータのいずれかを使って固有のAPIキーを含める必要があります。ここでは両方の方法を見ていきましょう。
知っておきたいポイント:
requestsパッケージはすでにインポート済みです。- 認証にはAPIキー/トークン
8apDFHaNJMxy8Kt818aa6b4a0ed0514b5d3を使用してください。
この演習はコースの一部です
Pythonを使ったAPI入門
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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')