Base64 身份验证 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18139093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Base64 Authentication Python
提问by Marcus
I'm following an api and I need to use a Base64 authentication of my User Id and password.
我正在关注一个 api,我需要使用我的用户 ID 和密码的 Base64 身份验证。
'User ID and Password need to both be concatenated and then Base64 encoded'
'用户 ID 和密码都需要连接,然后 Base64 编码'
it then shows the example
然后它显示了这个例子
'userid:password'
It then proceeds to say 'Provide the encoded value in an "Authorization Header"'
然后继续说“在“授权标头”中提供编码值”
'for example: Authorization: BASIC {Base64-encoded value}'
'例如:授权:BASIC {Base64-encoded value}'
How do I write this into a python api request?
如何将其写入 python api 请求?
z = requests.post(url, data=zdata )
Thanks
谢谢
采纳答案by Alfageme
You can encode the data and make the request by doing the following:
您可以通过执行以下操作对数据进行编码并发出请求:
import requests, base64
usrPass = "userid:password"
b64Val = base64.b64encode(usrPass)
r=requests.post(api_URL,
headers={"Authorization": "Basic %s" % b64Val},
data=payload)
I'm not sure if you've to add the "BASIC" word in the Authorization field or not. If you provide the API link, It'd be more clear.
我不确定您是否必须在授权字段中添加“BASIC”字样。如果您提供API链接,那就更清楚了。
回答by Josh
The requests library has Basic Auth supportand will encode it for you automatically. You can test it out by running the following in a python repl
requests 库具有Basic Auth 支持,并会自动为您编码。您可以通过在 python repl 中运行以下命令来测试它
from requests.auth import HTTPBasicAuth
r = requests.post(api_URL, auth=HTTPBasicAuth('user', 'pass'), data=payload)
You can confirm this encoding by typing the following.
您可以通过键入以下内容来确认此编码。
r.request.headers['Authorization']
outputs:
输出:
u'Basic c2RhZG1pbmlzdHJhdG9yOiFTRG0wMDY4'
回答by Michal Lis
With python3, I have found a solution which is working for me:
使用 python3,我找到了一个对我有用的解决方案:
import base64
userpass = username + ':' + password
encoded_u = base64.b64encode(userpass.encode()).decode()
headers = {"Authorization" : "Basic %s" % encoded_u}
回答by T.M.
As explained in the Requests documentation https://2.python-requests.org/en/latest/user/authentication/
如请求文档https://2.python-requests.org/en/latest/user/authentication/ 中所述
Making requests with HTTP Basic Auth is very simple:
>>> from requests.auth import HTTPBasicAuth >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass')) <Response [200]>
In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:
>>> requests.get('https://api.github.com/user', auth=('user', 'pass')) <Response [200]>
Providing the credentials in a tuple like this is exactly the same as the HTTPBasicAuth example above.
使用 HTTP 基本身份验证发出请求非常简单:
>>> from requests.auth import HTTPBasicAuth >>> requests.get('https://api.github.com/user', auth=HTTPBasicAuth('user', 'pass')) <Response [200]>
事实上,HTTP Basic Auth 非常普遍,以至于 Requests 为使用它提供了一个方便的速记:
>>> requests.get('https://api.github.com/user', auth=('user', 'pass')) <Response [200]>
在像这样的元组中提供凭据与上面的 HTTPBasicAuth 示例完全相同。