Reputation: 587
Despite the number of posts regarding Poloniex / Python trading api access, I still can't figure out how to make this work on Python 3.6. Here is one version which, in my view, should word perfectly, but doesn't:
req['command'] = 'requestBalances'
req['nonce'] = int(time.time() * 1000)
post_data = urllib.parse.urlencode(req).encode('utf-8')
hmac_key = self.Secret.encode('utf-8')
sign = hmac.new(hmac_key, post_data, hashlib.sha512)
sign = sign.hexdigest()
headers = {
'Sign': sign,
'Key': self.APIKey
}
res = requests.post('https://poloniex.com/tradingApi', data=post_data, headers=headers)
If I run the above, with the correct api / secret codes, I get an "invalid command" error.
Interestingly, if I replace the requests.post function with :
req = urllib.request.Request(url='https://poloniex.com/tradingApi', data=post_data, headers=headers)
res = urllib.request.urlopen(req,timeout=5)
then I don't get an error, but just an empty bytes array (after res.read())
Any tips on how to make this work would be greatly appreciated.
Upvotes: 1
Views: 587
Reputation: 587
The solution is to include:
"Content-type": "application/x-www-form-urlencoded"
in the header, i.e. :
headers = {
"Content-type": "application/x-www-form-urlencoded",
'Sign': sign,
'Key': self.APIKey
}
Strangely, none of the other solutions I have seen has included this additional field, but there we go.
PS. the alternative using urllib.request still just returns an empty byte string.
Upvotes: 2