2017-04-17 42 views
0

我對Python很陌生,使用的是3.6版本。Python 3.6.1對於poloniex交易api的HMAC-SHA512的發佈請求

我希望我能寫的Python 3.6.1 POST請求的代碼HMAC-SHA512的poloniex交易API

這裏是poloniex文檔:https://poloniex.com/support/api/

顯然,這是正常的API不同的GET請求。這是我的參數。

import urllib.request 
import urllib.parse 
import hmac 
import hashlib 
import time 

key = '<api key>' 
secret = '<secret>' #must sign by your key's "secret" according to the HMAC-SHA512 method 
command = 'returnBalances' 
nonce = int(time.time()) 

接下來要做什麼? 我不明白如何用HMAC-SHA512 &如何正確發送POST請求。 謝謝。

+0

可能有用的是從你提供的API鏈接的文檔中,在第44行,https://pastebin.com/fbkheaRb中的'api_query'方法'sign = hmac.new(self.Secret,post_data,hashlib .sha512).hexdigest()'.. – davedwards

回答

0

幫你一個忙,並且使用HTTP模塊requests

使用requests,this answer向您展示瞭如何在Python中使用HMAC-SHA512和請求。

0

我試圖重現如下。但是,它看起來像我遇到了錯誤。

{'command': 'returnBalances', 'nonce': 1492523610} 
command=returnBalances&nonce=1492523610 
Traceback (most recent call last): 
    File "C:/Users/draft/Desktop/test3.py", line 21, in <module> 
    signing = hmac.new(secret, post_data, hashlib.sha512).hexdigest() 
    File "C:\Users\draft\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 144, in new 
    return HMAC(key, msg, digestmod) 
    File "C:\Users\draft\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 84, in __init__ 
    self.update(msg) 
    File "C:\Users\draft\AppData\Local\Programs\Python\Python36-32\lib\hmac.py", line 93, in update 
    self.inner.update(msg) 
TypeError: Unicode-objects must be encoded before hashing 

這是我的完整代碼後,從你的幫助研究。

import urllib.request 
import urllib.parse 
import hmac 
import hashlib 
import time 
import json 

t = int(time.time()) 
secret = b'<secret>' 
headers = { 'Key' : '<api key>', 
      'Sign': ''} 
url = 'https://poloniex.com/tradingApi' 
req={} 

req['command'] = 'returnBalances' 
req['nonce'] = int(time.time()) 
#print (req) 

post_data = urllib.parse.urlencode(req) 
#print (post_data) 
signing = hmac.new(secret, post_data, hashlib.sha512).hexdigest() 
#print (signing) 
headers['Sign'] = signing 

ret = urllib.request.Request(url, data, headers) 
#print (ret) 


ret = urllib.request.urlopen(ret) 
a = json.loads(ret.read()) 
#print (a) 

任何人都可以檢查我的代碼嗎? 我相信我用「urllib.parse.urlencode」編碼
非常感謝。