Python 3 哈希 HMAC-SHA512
Python 3 hash HMAC-SHA512
我正在为 https://poloniex.com/support/api/
编写一个机器人
public 方法都可以正常工作,但交易 API 方法需要一些额外的技巧:
All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi 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
我也尝试过执行以下操作,但没有帮助:
非常感谢任何帮助!
您传递给 requests.post
的有效负载必须是有效的查询字符串或与该查询字符串对应的字典。通常情况下,只传递一个 dict 并获取请求来为您构建查询字符串会更方便,但在这种情况下我们需要从查询字符串构建一个 HMAC 签名,因此我们使用标准 urlib.parse 模块来构建查询字符串。
烦人的是,urlib.parse.urlencode
函数 return 是一个文本字符串,因此我们需要将其编码为字节字符串,以使其能够被 hashlib 接受。使用的明显编码是 UTF-8:将仅包含纯 ASCII 的文本字符串编码为 UTF-8 将创建一个与等效的 Python 2 字符串相同的字节序列(当然 urlencode
将仅 return 纯 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)
我正在为 https://poloniex.com/support/api/
编写一个机器人public 方法都可以正常工作,但交易 API 方法需要一些额外的技巧:
All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi 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
我也尝试过执行以下操作,但没有帮助:
非常感谢任何帮助!
您传递给 requests.post
的有效负载必须是有效的查询字符串或与该查询字符串对应的字典。通常情况下,只传递一个 dict 并获取请求来为您构建查询字符串会更方便,但在这种情况下我们需要从查询字符串构建一个 HMAC 签名,因此我们使用标准 urlib.parse 模块来构建查询字符串。
烦人的是,urlib.parse.urlencode
函数 return 是一个文本字符串,因此我们需要将其编码为字节字符串,以使其能够被 hashlib 接受。使用的明显编码是 UTF-8:将仅包含纯 ASCII 的文本字符串编码为 UTF-8 将创建一个与等效的 Python 2 字符串相同的字节序列(当然 urlencode
将仅 return 纯 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)