BTCMarkets / API

API
120 stars 30 forks source link

Bad request error for POST requests in Python3 #62

Open yungsoros opened 6 years ago

yungsoros commented 6 years ago

Hi,

I keep getting "Bad request" errors when passing POST requests to the API. There's no issue with GETs, so I am fairly certain the problem lies with the request body. Here's my code snippet, using python3, the base64, hmac, hashlib and requests libraries.

uri='/order/history'
nonce = str(int(time.time()*1000))

payload = {'currency':'AUD', 'instrument':'BTC', limit:10, since:0}
data = "{\"currency\":\"AUD\",\"instrument\":\"BTC\",\"limit\":\"10\",\"since\":\"0\"}"

msg = uri + '\n' + nonce + '\n' + data
msg = msg.encode()

secret = base64.standard_b64decode(self.secret.encode('utf-8'))

sig = hmac.new(secret, msg, hashlib.sha512)
sig = base64.standard_b64encode(sig.digest()).decode('utf-8')

headers = { "Accept": "application/json",
            "Accept-Charset": "UTF-8",
            "Content-Type": "application/json",
            "apikey": self.key,
            "timestamp": nonce,
            "signature": sig}

requests.post(self.domain + uri, headers=headers, data=payload, verify=True)

Any ideas what I might be doing wrong? Any help is appreciated.

martin-nginio commented 6 years ago

Hi @yungsoros

The following sample Python should work: https://github.com/BTCMarkets/api-client-python/blob/master/btcmarkets.py

Please feel free to run the sample or use it as a reference implementation.

Thanks.

Regards, Martin