I am not a developer by trade, so please go easy on me.
I have been working on a Bittrex API as a way of teaching myself Python and API's. I managed to get it working in PHP with a lot of Stack Overflow searches before switching to Python 3.6. Now I am stuck again on a portion that requires hashing and signatures.
My functional PHP code:
<?php
$apikey='...';
$apisecret='...';
$nonce=time();
$uri='https://bittrex.com/api/v1.1/account/getbalance?
$apikey='.$apikey.'&nonce='.$nonce.'¤cy=BTC';
$sign=hash_hmac('sha512',$uri,$apisecret);
$ch = curl_init($uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('apisign:'.$sign));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$execResult = curl_exec($ch);
$data=curl_exec($ch);
echo $data
?>
Python equivalent:
import urllib.request
import json
import collections
import time
import hashlib
import hmac
nonce = str('{:10.0f}'.format(time.time()))
apikey = '...'
apisecret = '...'
BaseCurrency = 'BTC'
url = 'https://bittrex.com/api/v1.1/account/getbalance?
apikey='+apikey+'&nonce='+nonce+'¤cy='+BaseCurrency
sign = hmac.new(b'apisecret', b'url', hashlib.sha512).hexdigest()
request = urllib.request.Request(url, headers={"apisign" : sign})
balance = json.load(urllib.request.urlopen(request))
print(balance)
This returns {'success': False, 'message': 'INVALID_SIGNATURE', 'result': None}.
I have spent a couple days on this and have not been able to get passed it. Any help would be appreciated.
You must use
apikey = b'...'
apisecret = b'...'
instead of
apikey = '...'
apisecret = '...'
and
sign = hmac.new(apisecret, url, hashlib.sha512).hexdigest()
instead of
sign = hmac.new(b'apisecret', b'url', hashlib.sha512).hexdigest()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With