Requests-authentication

提供:Dev Guides
移動先:案内検索

リクエスト-認証

この章では、リクエストモジュールで使用できる認証のタイプについて説明します。

私たちは以下について議論します-

  • HTTPリクエストでの認証の機能
  • 基本認証
  • ダイジェスト認証
  • OAuth2認証

HTTPリクエストでの認証の機能

HTTP認証はサーバー側で行われ、クライアントがURLを要求すると、ユーザー名やパスワードなどの認証情報を要求します。 これは、クライアントとサーバー間で交換される要求と応答の追加のセキュリティです。

クライアント側から、これらの追加の認証情報、つまり ユーザー名とパスワードはヘッダーで送信でき、後でサーバー側で検証されます。 認証が有効な場合にのみ、サーバー側から応答が配信されます。

要求ライブラリは、基本認証(HTTPBasicAuth)およびダイジェスト認証(HTTPDigestAuth)である、requests.authで最も一般的に使用される認証を備えています。

基本認証

これは、サーバーに認証を提供する最も簡単な形式です。 基本認証を処理するために、リクエストライブラリで利用可能なHTTPBasicAuthクラスを使用します。

これは、それを使用する方法の実用的な例です。

import requests
from requests.auth import HTTPBasicAuth
response_data =
requests.get('httpbin.org/basic-auth/admin/admin123',
auth = HTTPDigestAuth('admin', 'admin123'))
print(response_data.text)

ユーザー名を_admin_、パスワードを_admin123_として、https://httpbin.org/basic-auth/admin/admin123というURLを呼び出しています。

したがって、このURLは認証なしでは機能しません。 ユーザーとパスワード。 auth paramを使用して認証を行うと、サーバーのみが応答を返します。

出力

E:\prequests>python makeRequest.py
{
   "authenticated": true,
   "user": "admin"
}

ダイジェスト認証

これは、リクエストで使用できるもう1つの認証形式です。 リクエストからHTTPDigestAuthクラスを利用します。

import requests
from requests.auth import HTTPDigestAuth
response_data =
requests.get('https://httpbin.org/digest-auth/auth/admin/admin123',
auth = HTTPDigestAuth('admin', 'admin123'))
print(response_data.text)

出力

E:\prequests>python makeRequest.py
{
   "authenticated": true,
   "user": "admin"
}

OAuth2認証

OAuth2認証を使用するには、「requests_oauth2」ライブラリが必要です。 「requests_oauth2」をインストールするには、次のようにします-

pip install requests_oauth2

インストール中の端末の表示は次のようになります-

E:\prequests>pip install requests_oauth2
Collecting requests_oauth2
Downloading https://files.pythonhosted.org/packages/52/dc/01c3c75e6e7341a2c7a9
71d111d7105df230ddb74b5d4e10a3dabb61750c/requests-oauth2-0.3.0.tar.gz
Requirement already satisfied: requests in c:\users\xyz\appdata\local\programs
\python\python37\lib\site-packages (from requests_oauth2) (2.22.0)
Requirement already satisfied: six in c:\users\xyz\appdata\local\programs\pyth
on\python37\lib\site-packages (from requests_oauth2) (1.12.0)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in c:\use
rs\xyz\appdata\local\programs\python\python37\lib\site-packages (from requests
->requests_oauth2) (1.25.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\xyz\appdata\loca
l\programs\python\python37\lib\site-packages (from requests->requests_oauth2) (2
019.3.9)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in c:\users\xyz\appdata\l
ocal\programs\python\python37\lib\site-packages (from requests->requests_oauth2)
(3.0.4)
Requirement already satisfied: idna<2.9,>=2.5 in c:\users\xyz\appdata\local\pr
ograms\python\python37\lib\site-packages (from requests->requests_oauth2) (2.8)
Building wheels for collected packages: requests-oauth2
Building wheel for requests-oauth2 (setup.py) ... done
Stored in directory: C:\Users\xyz\AppData\Local\pip\Cache\wheels\90\ef\b4\43
3743cbbc488463491da7df510d41c4e5aa28213caeedd586
Successfully built requests-oauth2

「requests-oauth2」のインストールが完了しました。 Google、TwitterのAPIを使用するには、同意が必要です。OAuth2認証を使用して同じことが行われます。

OAuth2認証では、クライアントIDと秘密鍵が必要です。 取得方法の詳細については、https://developers.google.com/identity/protocols/OAuth2で説明されています。

後で、https://console.developers.google.com/にあるGoogle API Consoleにログインし、クライアントIDと秘密鍵を取得します。

「requests-oauth2」の使用例を以下に示します。

import requests
from requests_oauth2.services import GoogleClient
google_auth = GoogleClient(
   client_id="xxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
   redirect_uri="http://localhost/auth/successl",
)
a = google_auth.authorize_url(
   scope=["profile", "email"],
   response_type="code",
)
res = requests.get(a)
print(res.url)

Gmailアカウントにログインする必要があるため、指定されたURLにリダイレクトすることはできませんが、この例では、google_authが機能し、承認されたURLが指定されていることがわかります。

出力

E:\prequests>python oauthRequest.py
https://accounts.google.com/o/oauth2/auth?redirect_uri=
http%3A%2F%2Flocalhost%2Fauth%2Fsuccessl&
client_id=xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&
scope=profile+email&response_type=code