python-3.xpoloniex

Python Poloniex API returns error 422


I have method, that should returns balance from Poloniex via their API. Code below.

 def getBalance(self):
        polo_data = {"command": "returnBalances", "nonce": int(time.time() * 1000)}
        post_data = urllib.parse.urlencode(polo_data).encode()
        sig =  hmac.new(str.encode(self.SETTINGS.PRIVATE_KEY), post_data, hashlib.sha512).hexdigest()

        headers = {
            "Key": self.SETTINGS.PUBLIC_KEY,
            "Sign": sig}
        req = urllib.request.Request(self.SETTINGS.BASE_URI + self.SETTINGS.TRADE_DIR,
                                           data=post_data, headers=headers)
        try:
            res = urllib.request.urlopen(req)
            result =  json.loads(res.read().decode('utf-8'))
            print(result)
        except urllib.error.HTTPError as e:
            print(f'Headers: {e.headers}')
            print(f'Status: {e.status}')
            print(f'MSG: {e.msg}')
            print(f'reason: {e.reason}')
            print(f'url: {e.url}')
            print(f'Name: {e.name}')

When I run this code I return message HTTPError 422 I thinking problem would be with post_data. I tried do the same with requests and I got error with message: "Error: Invalid command" But I can't understand where exactly. From other side the same code for buy/sell order's commands working like a charm.

I using Python 3.6.1

Any suggestions?


Solution

  • I found decision for my question. Problem was in nonce. Correct code is:

    int(time.time() * 10000) 
    

    not a 1000, but 10000