📜  Requests-身份验证

📅  最后修改于: 2020-10-21 08:35:14             🧑  作者: Mango


 

本章将讨论“请求”模块中可用的身份验证类型。

我们将讨论以下内容-

  • HTTP请求中身份验证的工作
  • 基本认证
  • 摘要身份验证
  • OAuth2身份验证

HTTP请求中身份验证的工作

服务器端上的HTTP身份验证在客户端请求URL时要求一些身份验证信息,例如用户名,密码。这是在客户端和服务器之间交换请求和响应的附加安全性。

从客户端可以在标头中发送这些其他身份验证信息(即用户名和密码),稍后将在服务器端进行验证。仅当身份验证有效时,才会从服务器端传递响应。

请求库在request.auth中具有最常用的身份验证,这是基本身份验证( HTTPBasicAuth )和摘要身份验证( HTTPDigestAuth )。

基本认证

这是向服务器提供身份验证的最简单形式。为了使用基本身份验证,我们将使用请求库中可用的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的URL调用https://httpbin.org/basic-auth/admin/admin123

因此,如果没有身份验证(即用户名和密码),此URL将无法工作。使用auth参数进行身份验证后,只有服务器会返回响应。

输出

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

摘要式身份验证

这是请求中可用的另一种身份验证形式。我们将利用请求中的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的API,Twitter我们需要征得其同意,并且使用OAuth2身份验证也是如此。

对于OAuth2身份验证,我们将需要客户端ID和密钥。有关获取方法的详细信息,请参见https://developers.google.com/identity/protocols/OAuth2

稍后,登录https://console.developers.google.com/上的Google API控制台,并获取客户端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/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://accounts.google.com/o/oauth2/auth?redirect_uri=
http%3A%2F%2Flocalhost%2Fauth%2Fsuccess.html&
client_id=xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com&
scope=profile+email&response_type=code