我正在寫https://poloniex.com/support/api/的Python 3哈希HMAC-SHA512
公共方法都做工精細一個機器人,但交易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
我也試圖執行以下,但它並沒有幫助: https://stackoverflow.com/a/25111089/7317891
任何幫助,不勝感激!
你'payload'在'returnBalances'是一個字典,它不支持緩衝的API(閱讀:便宜的字節表示)。您可能需要'payload = str(dict(command ='returnBalances',nonce = ...))',因爲字符串的表示形式類似於這種情況恰好等於JSON表示形式;它的字節編碼形式可以發送給HMAC。 – user2722968
@ user2722968 Python字典的字符串表示形式不是有效的查詢字符串,所以這不起作用。 –