Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中由TOTP生成密码的情况下发出HTTP基本身份验证post请求?_Python_Http_Authentication_Post - Fatal编程技术网

如何在Python中由TOTP生成密码的情况下发出HTTP基本身份验证post请求?

如何在Python中由TOTP生成密码的情况下发出HTTP基本身份验证post请求?,python,http,authentication,post,Python,Http,Authentication,Post,任务是通过发送json字符串数据向url发出HTTP post请求,url受HTTP基本身份验证保护,我需要在标题中提供一个授权:字段,emailAdd是基本身份验证的用户ID,密码由TOTP生成,其中数字为10位,时间步长为30秒,T0为0,哈希函数使用sha512,共享密钥为emailAdd+“ABCD” 我的代码如下: import requests, base64, json from passlib.totp import TOTP from requests.auth import

任务是通过发送json字符串数据向url发出HTTP post请求,url受HTTP基本身份验证保护,我需要在标题中提供一个授权:字段,emailAdd是基本身份验证的用户ID密码由TOTP生成,其中数字为10位,时间步长为30秒,T0为0,哈希函数使用sha512,共享密钥为emailAdd+“ABCD”

我的代码如下:

import requests, base64, json
from passlib.totp import TOTP
from requests.auth import HTTPBasicAuth


totp = TOTP(key = base64.b32encode(shared_secret.encode()), digits = 10, alg = "sha512")
password = totp.generate().token

r = requests.Session()
#auth = base64.b64encode((''.join(userid) + ':' + ''.join(password)).encode())
r.headers.update({"Content-Type": "application/json"})      #this is task required
#r.headers.update({"Authorization": "Basic " + auth.decode()})

res = r.post(url, json = data, auth = (userid, password))
print(res.status_code)
print(res.content)
但是我的身份验证失败了,我认为密码应该是正确的,post请求有问题,有人能告诉我是什么问题吗?另外,我与服务器处于不同的时区,这对TOTP生成的密码有什么影响吗?如果有必要的话,我正在windows上运行python


感谢在
请求中使用
HTTPBasicAuth
,您只需使用
r.post(url、数据、身份验证=(用户名、密码))
。此外,要传递json数据,您可以使用
r.post(url,json=一个对象,如\u dict\u或\u list,auth=(用户名、密码))
。您不需要手动修改标题。@Srwa谢谢您,我已经修改了代码,但这不是身份验证失败的原因,您有什么想法吗?要在
请求中使用
HTTPBasicAuth
,您只需使用
r.post(url,数据,auth=(用户名,密码))
。此外,要传递json数据,您可以使用
r.post(url,json=一个对象,如\u dict\u或\u list,auth=(用户名、密码))
。您不需要手动修改标题。@Srwa谢谢您,我已经修改了代码,但这不是身份验证失败的原因,您有什么想法吗?