我試圖重現如下。但是,它看起來像我遇到了錯誤。
{'command': 'returnBalances', 'nonce': 1492523610}
command=returnBalances&nonce=1492523610
Traceback (most recent call last):
File "C:/Users/draft/Desktop/test3.py", line 21, in <module>
signing = hmac.new(secret, post_data, hashlib.sha512).hexdigest()
File "C:\Users\draft\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 144, in new
return HMAC(key, msg, digestmod)
File "C:\Users\draft\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 84, in __init__
self.update(msg)
File "C:\Users\draft\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 93, in update
self.inner.update(msg)
TypeError: Unicode-objects must be encoded before hashing
這是我的完整代碼後,從你的幫助研究。
import urllib.request
import urllib.parse
import hmac
import hashlib
import time
import json
t = int(time.time())
secret = b'<secret>'
headers = { 'Key' : '<api key>',
'Sign': ''}
url = 'https://poloniex.com/tradingApi'
req={}
req['command'] = 'returnBalances'
req['nonce'] = int(time.time())
#print (req)
post_data = urllib.parse.urlencode(req)
#print (post_data)
signing = hmac.new(secret, post_data, hashlib.sha512).hexdigest()
#print (signing)
headers['Sign'] = signing
ret = urllib.request.Request(url, data, headers)
#print (ret)
ret = urllib.request.urlopen(ret)
a = json.loads(ret.read())
#print (a)
任何人都可以檢查我的代碼嗎? 我相信我用「urllib.parse.urlencode」編碼
非常感謝。
可能有用的是從你提供的API鏈接的文檔中,在第44行,https://pastebin.com/fbkheaRb中的'api_query'方法'sign = hmac.new(self.Secret,post_data,hashlib .sha512).hexdigest()'.. – davedwards