2017-04-04 28 views
0

我正在嘗試使用我的Poloniex API密鑰和密鑰來檢查我帳戶上的餘額。但是,我一直收到「invalid command」作爲迴應。爲什麼從Python 3的Poloniex API中獲得'invalid command'?

下面是我在Python3代碼:

 command = 'returnBalances' 
     req['command'] = command 
     req['nonce'] = int(time.time()*1000) 
     post_data = urllib.parse.urlencode(req).encode() 

     sign = hmac.new(str.encode(self.Secret), post_data, hashlib.sha512).hexdigest() 
     headers = { 
      'Sign': sign, 
      'Key': self.APIKey 
     } 

     print(post_data) 
     req = urllib.request.Request(url='https://poloniex.com/tradingApi', headers=headers) 
     res = urllib.request.urlopen(req, timeout=20) 

     jsonRet = json.loads(res.read().decode('utf-8')) 
     return self.post_process(jsonRet) 

print(post_data)回報什麼,我希望看到:

b'nonce=1491334646563&command=returnBalances' 
+0

看起來你是不是送'post_data'與請求(我假設你必須在POST體發送)。 –

回答

0

我猜你一定與請求發送post_data。我喜歡直接使用requests庫而不是urllib,但它應該是這樣用普通urllib

req = urllib.request.Request('https://poloniex.com/tradingApi', post_data, headers) 
+0

謝謝!我認爲post_data包含在「sign =」行中的標題中。我對這個urllib的東西很陌生,我只是試圖破解一個預先存在的API包裝器來使用python 3。 – Olgo

2

發送Content-Type頭

excellent article向我指出了正確的方向。它顯示python 3的請求庫跳過發送Content-Type標題,這導致Polo拒絕請求。

'Content-Type': 'application/x-www-form-urlencoded' 

標題:

headers = { 
    'Sign': hmac.new(SECRET.encode(), post_data, hashlib.sha512).hexdigest(), 
    'Key': API_KEY, 
    'Content-Type': 'application/x-www-form-urlencoded' 
} 
相關問題