a中的HMAC apisign请求标头,但标头必须为str错误?

2024-09-30 02:19:39 发布

您现在位置:Python中文网/ 问答频道 /正文

    apikey = '$mykey'
    apisecret = '$mykeysecret'
    nonce = str(int(time.time() * 1000))
    uri = 'https://somewebsite.com/api/v1.5/account/getbalances?apikey='+apikey+'&nonce='+nonce
    sign = hmac.new(b'apisecret', b'uri', hashlib.sha512)
    response = requests.get(uri, headers={'apisign': sign}, timeout=10)
    print(json.dumps(response, sort_keys=True, indent=4))

 """ The following are the example provided by the website """
    # $ch = curl_init($uri);
    # curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
    # $execResult = curl_exec($ch);
    # $obj = json_decode($execResult);
    # url = "https://somewebsite.com/api/v1.5/account/getbalances?apikey=API_KEY"

经过两个小时的研究,我无法解决这个问题。现在它说头必须是str。但是使用str{'apisign':sign}它说str对象没有项


Tags: httpscomapitimeaccounturicurlch
1条回答
网友
1楼 · 发布于 2024-09-30 02:19:39

您正在将字典正确地传递到headers参数中,但错误是sign的值不是字符串sign是HMAC对象,需要转换为字符串:

>>> print(sign)
<hmac.HMAC object at 0x03337950>

我将查看调用sign.digest()以查看它是否返回您期望的字符串。如果是,则可以用headers={'apisign': sign.digest()}替换headers={'apisign': sign}

相关问题 更多 >

    热门问题