【问题标题】:I am trying to make an HTTP post, but json returns "message":"Unauthorized"我正在尝试发布 HTTP 帖子,但 json 返回 "message":"Unauthorized"
【发布时间】:2020-04-11 15:50:03
【问题描述】:
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

【问题讨论】:

  • 嗨 Toufiqur,我也遇到了同样的问题,你找到为什么这不起作用了吗? :(
  • @SuperKogito 是的。使用邮递员,在授权下找到基本身份验证并输入您的电子邮件ID和随机密码...然后选择POST类型并单击发送
  • @ToufiqurRahman 邮递员和蟒蛇?
  • @Math_Avengers 你能说清楚你想知道什么吗?

标签: python json api


【解决方案1】:

该 URL 受 HTTP 基本身份验证保护,这在 RFC2617 的第 2 章中有说明,因此您必须在 POST 请求中提供 Authorization: 标头字段 对于 HTTP 基本身份验证的用户 ID,请使用您在 >JSON 字符串中输入的相同电子邮件地址。对于密码,请提供一个 10 位基于时间的一次性密码>符合 RFC6238 TOTP。

正如这里提到的,您必须提供一个 Authorization: 头字段,如下所示:

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

您可以从https://github.com/settings/tokens获取 GitHub 的 OATH Token

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-11
    相关资源
    最近更新 更多