如何在Python中使用TOTP生成密码的HTTP基本身份验证发布请求?

时间:2017-10-09 03:25:19

标签: python http authentication post

任务是向 url 发送HTTP发布请求,发送json字符串数据 url 受HTTP基本身份验证保护,我需要在标头中提供授权:字段, emailAdd 是基本身份验证的用户ID 密码是由TOTP生成,其中数字为10位,时间步长为30s,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)

但我认证失败,我认为密码应该是正确的,并且发帖请求有问题,有谁能告诉我这是什么问题?另外,我在与服务器不同的时区,是否对TOTP生成的密码有任何影响?而且我在Windows上运行python,如果这很重要的话。

由于

0 个答案:

没有答案
相关问题