尽管关于Poloniex / Python交易api访问的帖子数量很多,但我仍然无法弄清楚如何在Python 3.6上使用它。这是一个版本,在我看来,应该完美地说,但不是:
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)
如果我运行上面的代码,使用正确的api /密码,我会收到“无效命令”错误。
有趣的是,如果我将requests.post函数替换为:
req = urllib.request.Request(url='https://poloniex.com/tradingApi', data=post_data, headers=headers)
res = urllib.request.urlopen(req,timeout=5)
然后我没有得到错误,只是一个空字节数组(在res.read()之后)
非常感谢任何有关如何完成这项工作的提示。
答案 0 :(得分:2)
解决方案包括:
"Content-type": "application/x-www-form-urlencoded"
标题中的,即:
headers = {
"Content-type": "application/x-www-form-urlencoded",
'Sign': sign,
'Key': self.APIKey
}
奇怪的是,我见过的其他解决方案都没有包含这个额外的字段,但我们去了。
PS。使用urllib.request的替代方法仍然只返回一个空字节字符串。