2017-05-31 170 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的替代方法仍然只返回一個空字節字符串。