python - 我正在尝试发送 HTTP 帖子,但 json 返回 "message":"Unauthorized"

标签 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)

授权

The URL is protected by HTTP Basic Authentication, which is explained on Chapter 2 of RFC2617, so you have to provide an Authorization: header field in your POST request

For the userid of HTTP Basic Authentication, use the same email address you put in the JSON string. For the password, provide a 10-digit time-based one time password conforming to RFC6238 TOTP.

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

最佳答案

The URL is protected by HTTP Basic Authentication, which is explained on Chapter 2 >of RFC2617, so you have to provide an Authorization: header field in your POST request For the userid of HTTP Basic Authentication, use the same email address you put in >the JSON string. For the password, provide a 10-digit time-based one time password >conforming to RFC6238 TOTP.

如此处所述,您必须像这样提供 Authorization: header 字段:

headers={"Authorization":"OATH TOKEN","content-type": "application/json"}

您可以从 https://github.com/settings/tokens 获得 github 的 OATH token

关于python - 我正在尝试发送 HTTP 帖子,但 json 返回 "message":"Unauthorized",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61159566/

相关文章:

python - 计算每个时间步长的可变长度输出的成本

python - 在python中读取TDMS文件如何使用tdms info命令?

Python:使用 PyCharm 和 IDLE/python 时的不同结果

json - Django 请求 Post json

java - API 中的碱基转换方法是什么?

javascript - 如何在 Pinterest 上分享 data-uri 图像?

python - 错误的代码或奇怪的数学行为? ( python 3)

ios - 解析不同类型的 json 响应

javascript - 确保不存在重复的目录路径

javascript - Google Maps API 检查标记是否存在于多个边界