Python 3哈希HMAC-SHA512
这个问题已经在这里有了答案:            >             How do I sign a POST request using HMAC-SHA512 and the Python requests library?                                    1个
我正在为 https://poloniex.com/support/api/写一个机器人

公用方法都可以正常工作,但是Trading API方法需要一些额外的技巧:

All calls to the trading API are sent via HTTP POST to 07002 and must contain the following headers:
Key – Your API key.
Sign – The query’s POST data signed by your key’s “secret” according to the HMAC-SHA512 method.
Additionally, all queries must include a “nonce” POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used.
All responses from the trading API are in JSON format.

我的returnBalances代码如下所示:

import hashlib
import hmac
from time import time

import requests


class Poloniex:
    def __init__(self, APIKey, Secret):
        self.APIKey = APIKey
        self.Secret = Secret

    def returnBalances(self):
        url = 'https://poloniex.com/tradingApi'
        payload = {
            'command': 'returnBalances',
            'nonce': int(time() * 1000),
        }

        headers = {
            'Key': self.APIKey,
            'Sign': hmac.new(self.Secret, payload, hashlib.sha512).hexdigest(),
        }

        r = requests.post(url, headers=headers, data=payload)
        return r.json()

trading.py:

APIkey = 'AAA-BBB-CCC'
secret = b'123abc'

polo = Poloniex(APIkey, secret)
print(polo.returnBalances())

我得到了以下错误:

Traceback (most recent call last):
  File "C:/Python/Poloniex/trading.py", line 5, in <module>
    print(polo.returnBalances())
  File "C:\Python\Poloniex\poloniex.py", line 22, in returnBalances
    'Sign': hmac.new(self.Secret, payload, hashlib.sha512).hexdigest(),
  File "C:\Users\Balazs91\AppData\Local\Programs\Python\Python35-32\lib\hmac.py", line 144, in new
    return HMAC(key, msg, digestmod)
  File "C:\Users\Balazs91\AppData\Local\Programs\Python\Python35-32\lib\hmac.py", line 84, in __init__
    self.update(msg)
  File "C:\Users\Balazs91\AppData\Local\Programs\Python\Python35-32\lib\hmac.py", line 93, in update
    self.inner.update(msg)
TypeError: object supporting the buffer API required

Process finished with exit code 1

我也尝试实现以下方法,但没有帮助:
https://stackoverflow.com/a/25111089/7317891

任何帮助深表感谢!

最佳答案
传递给request.post的有效负载必须是有效的查询字符串或与该查询字符串相对应的字典.通常,传递一个dict并获取请求以为您构建查询字符串会更方便,但是在这种情况下,我们需要根据查询字符串构造一个HMAC签名,因此我们使用标准的urlib.parse模块来构建查询串.

令人讨厌的是,urlib.parse.urlencode函数返回文本字符串,因此我们需要将其编码为字节字符串,以使其可被hashlib接受.使用的显而易见的编码是UTF-8:对仅包含纯ASCII的文本字符串进行编码,因为UTF-8将创建与等效的Python 2字符串相同的字节序列(当然urlencode将仅返回纯ASCII),因此此代码的行为与您链接的Poloniex API页面上的旧Python 2代码相同.

from time import time
import urllib.parse
import hashlib
import hmac

APIkey = b'AAA-BBB-CCC'
secret = b'123abc'

payload = {
    'command': 'returnBalances',
    'nonce': int(time() * 1000),
}

paybytes = urllib.parse.urlencode(payload).encode('utf8')
print(paybytes)

sign = hmac.new(secret, paybytes, hashlib.sha512).hexdigest()
print(sign)

输出

b'command=returnBalances&nonce=1492868800766'
3cd1630522382abc13f24b78138f30983c9b35614ece329a5abf4b8955429afe7d121ffee14b3c8c042fdaa7a0870102f9fb0b753ab793c084b1ad6a3553ea71

然后你可以做类似的事情

headers = {
    'Key': APIKey,
    'Sign': sign,
}

r = requests.post(url, headers=headers, data=paybytes)
点击查看更多相关文章

转载注明原文:Python 3哈希HMAC-SHA512 - 乐贴网