python-3.xpython-requestspoloniex

Requests for Poloniex API


I trying work with Poloniex API. And I try get balances via Trading API methods. And I try do it with requests library like this:

import requests
import hmac
import hashlib
import time
import urllib

def setPrivateCommand(self):
    poloniex_data = {'command': 'returnBalances', 'nonce': int(time.time() * 1000)}
    post_data = urllib.parse.urlencode(poloniex_data).encode()
    sig = hmac.new(str.encode(app.config['HMAC_KEYS']['Poloniex_Secret']), post_data, hashlib.sha512).hexdigest()
    headers = {'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey']}
    polo_request = requests.post('https://poloniex.com/tradingApi', data=post_data, headers=headers, timeout=20)
    polo_request = polo_request.json()
    print('Request: {0}'.format(polo_request))
    return polo_request

With this code I always get error with message: "Request: {'error': 'Invalid command.'}". What I do wrong?

From other side code below returns data without any problem! Look at this, please:

import requests
import hmac
import hashlib
import json
import time
import urllib

def setPrivateCommand(self):
    poloniex_data = {'command': 'returnBalances', 'nonce': int(time.time() * 1000)}
    post_data = urllib.parse.urlencode(poloniex_data).encode()
    sig = hmac.new(str.encode(app.config['HMAC_KEYS']['Poloniex_Secret']), post_data, hashlib.sha512).hexdigest()
    headers = {'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey']}
    req = urllib.request.Request('https://poloniex.com/tradingApi', data=post_data, headers=headers)
    res = urllib.request.urlopen(req, timeout=20)
    Ret_data = json.loads(res.read().decode('utf-8'))
    print('Request: {0}'.format(Ret_data))
    return Ret_data

I using Python 3.6


Solution

  • It's best to let requests handle post data because it creates the appropriate headers. Other than that i don't see anything wrong with your code.

    def setPrivateCommand(self):
        poloniex_data = {'command': 'returnBalances', 'nonce': int(time.time() * 1000)}
        post_data = urllib.parse.urlencode(poloniex_data).encode()
        sig = hmac.new(
            str.encode(app.config['HMAC_KEYS']['Poloniex_Secret']), post_data, hashlib.sha512
        ).hexdigest()
        headers = {'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey']}
        polo_request = requests.post(
            'https://poloniex.com/tradingApi', data=poloniex_data, headers=headers, timeout=20
        )
        polo_request = polo_request.json()
        print('Request: {0}'.format(polo_request))
        return polo_request 
    

    Or you could specify the 'Content-Type' in headers if you want to have a string in data, example,

    headers = {
        'Sign': sig, 'Key': app.config['HMAC_KEYS']['Poloniex_APIKey'], 
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    polo_request = requests.post(
        'http://httpbin.org/anything', data=post_data, headers=headers, timeout=20
    )