Python-Poloniex交易api问题
尽管有很多关于Poloniex/Python交易api访问的帖子,我仍然不知道如何在Python3.6上实现这一点。在我看来,这里有一个版本应该是完美的,但不是:Python-Poloniex交易api问题,python,api,python-requests,urllib,Python,Api,Python Requests,Urllib,尽管有很多关于Poloniex/Python交易api访问的帖子,我仍然不知道如何在Python3.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')
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()之后)
如果您有任何关于如何完成此任务的建议,我们将不胜感激。解决方案包括:
"Content-type": "application/x-www-form-urlencoded"
在标题中,即:
headers = {
"Content-type": "application/x-www-form-urlencoded",
'Sign': sign,
'Key': self.APIKey
}
奇怪的是,我所看到的其他解决方案都没有包括这个额外的领域,但我们开始了
另外,使用urllib.request的替代方法仍然只返回一个空字节字符串。您能分享您收到的完整回溯吗?纯属运气,我找到了一个解决方案。见下面的答案