2017-04-04 3 views
0

私のPoloniex APIの秘密とキーを使用して、アカウントの残高を確認しようとしています。しかし、「invalid command」が応答として返され続けます。以下はPoloniex APIからPython 3で「無効なコマンド」が表示されるのはなぜですか?

の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のボディにそれを送信しなければならないと仮定)と 'post_data'を送信していないように見えます。 –

答えて

0

私はあなたが要求してpost_dataを送信しなければならないと思います。私が代わりに直接urllibrequestsライブラリを使用したいが、それは、プレーンurllibでこのような何かをする必要があります:

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

ありがとう!私はpost_dataがヘッダーに "sign ="行に含まれていると考えました。私はこのurllibのものと私は非常に新しいPython 3で動作するように既存のAPIラッパーをハックしようとしています。 – Olgo

2

このexcellent articleが正しい方向に私を指摘Content-Typeヘッダ

を送信します。 python 3のリクエストライブラリがContent-Typeヘッダーの送信をスキップして、ポロがリクエストを拒否するように見えます。

'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' 
} 
関連する問題