I have tried to reproduce as follow. However, it looks like i encounter the error.
{'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
Here is my full code after doing study from your help.
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)
Can anyone please check my code?
I believe i do the encoding with "urllib.parse.urlencode"
Thank you very much.