2017-05-31 66 views
1

尽管有关Poloniex/Python交易API访问的帖子数量,我仍然无法弄清楚如何使Python 3.6的这项工作。这里是一个版本,它在我看来,这个词应该完美,但并不:Python - Poloniex交易API问题

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

您能分享您收到的完整回溯吗? – etemple1

+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的替代方法仍然只返回一个空字节字符串。