我正在尝试发布HTTP帖子,但是json返回“消息”:“未经授权”

时间:2020-04-11 15:50:03

标签: python json api

import httplib2
import hmac
import hashlib
import time
import sys
import struct
import json

root = "https://api.challenge.hennge.com/challenges/003"
content_type = "application/json"
userid = "toufiqurrahman45@gmail.com"
name = "HENNGECHALLENGE003"
shared_secret = userid+name

timestep = 30
T0 = 0

def HOTP(K, C, digits=10):
    K_bytes = str.encode(K)
    C_bytes = struct.pack(">Q", C)
    hmac_sha512 = hmac.new(key = K_bytes, msg=C_bytes, digestmod=hashlib.sha512).hexdigest()
    return Truncate(hmac_sha512)[-digits:]

def Truncate(hmac_sha512):
    offset = int(hmac_sha512[-1], 16)
    binary = int(hmac_sha512[(offset *2):((offset*2)+8)], 16) & 0x7FFFFFFF
    return str(binary)

def TOTP(K, digits=10, timeref = 0, timestep = 30):
    C = int ( time.time() - timeref ) // timestep
    return HOTP(K, C, digits = digits)

data = { "github_url": "https://gist.github.com/TaufiqurRahman45/f5347e5ba6e7d24aa61ac8c78fda452e", "contact_email": "toufiqurrahman45@gmail.com" }

password = TOTP(shared_secret, 10, T0, timestep).zfill(10) 

h = httplib2.Http()
h.add_credentials( userid, password )
header = {"content-type": "application/json"}
resp, content = h.request(root, "POST", headers = header, body = json.dumps(data))
print(resp)
print(content)

授权

URL受HTTP基本身份验证的保护,这在RFC2617的第2章中进行了说明,因此您必须在POST请求中提供一个Authorization:标头字段

对于HTTP基本认证的用户ID,请使用您在JSON字符串中输入的相同电子邮件地址。 对于密码,请提供符合RFC6238 TOTP的10位基于时间的一次性密码。

我还生成密码并使用哈希函数HMAC-SHA-512

0 个答案:

没有答案
相关问题