請求 - 身份驗證



本章將討論 Requests 模組中可用的身份驗證型別。

我們將討論以下內容 -

  • HTTP 請求中的身份驗證工作原理
  • 基本身份驗證
  • 摘要身份驗證
  • OAuth2 身份驗證

HTTP 請求中的身份驗證工作原理

HTTP 身份驗證是在伺服器端,當客戶端請求 URL 時,要求提供一些身份驗證資訊,例如使用者名稱和密碼。這是對客戶端和伺服器之間交換的請求和響應的額外安全措施。

在客戶端,這些額外的身份驗證資訊(即使用者名稱和密碼)可以傳送在請求頭中,隨後在伺服器端進行驗證。只有在身份驗證有效的情況下,伺服器端才會提供響應。

Requests 庫在 requests.auth 中包含最常用的身份驗證,分別是基本身份驗證(HTTPBasicAuth)和摘要身份驗證(HTTPDigestAuth)。

基本身份驗證

這是向伺服器提供身份驗證的最簡單形式。要使用基本身份驗證,我們將使用 Requests 庫提供的 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)  

我們正在呼叫 URL https://httpbin.org/basic-auth/admin/admin123,其中使用者為admin,密碼為admin123

因此,此 URL 在沒有身份驗證(即使用者和密碼)的情況下將無法工作。只有在使用 auth 引數提供身份驗證後,伺服器才會返回響應。

輸出

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

摘要身份驗證

這是 Requests 提供的另一種身份驗證形式。我們將使用 Requests 中的 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 身份驗證,我們將需要 Client ID 和 Secret Key。如何獲取它們的詳細資訊,請參閱https://developers.google.com/identity/protocols/OAuth2

之後,登入到 Google API 控制檯(位於 https://console.developers.google.com/)並獲取 Client ID 和 Secret Key。

示例

這是一個關於如何使用“requests-oauth2”的示例。

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

我們將無法重定向到給定的 URL,因為它需要登入 Gmail 帳戶,但是在這裡,您將從示例中看到 google_auth 可以工作,並且提供了授權的 URL。

輸出

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

© . All rights reserved.