BTCMarkets / api-client-python

16 stars 8 forks source link

Authentication error - {'success': False, 'errorCode': 1, 'errorMessage': 'Authentication failed.'} #8

Open Tallthunder opened 6 years ago

Tallthunder commented 6 years ago

Keep getting the above code. Not sure why. I would appreciate any help.

Here is the code I am using ( Its not my public/private API keys as I have checked them multiple times ):

import base64, hashlib, hmac, time, urllib, urllib.request, json from collections import OrderedDict

base_url = 'https://api.btcmarkets.net'

def request(action, key, signature, timestamp, path, data):

header = {
    'accept': 'application/json', 
    'Content-Type': 'application/json',
    'User-Agent': 'btc markets python client',
    'accept-charset': 'utf-8',  
    'apikey': key,
    'signature': signature,
    'timestamp': timestamp,
}

request = urllib.request.Request(base_url + path, data, header)
if action == 'post':
    response = urllib.request.urlopen(request, data)
else:
    response = urllib.request.urlopen(request)  
return json.loads(response.read().decode('utf-8'))

def get_request(key, secret, path):

nowInMilisecond = str(int(time.time() * 1000))
stringToSign = path + "\n" + nowInMilisecond + "\n"  

signature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest())

return request('get', key, signature, nowInMilisecond, path, None)    

def post_request(key, secret, path, postData):

nowInMilisecond = str(int(time.time() * 1000))
stringToSign = path + "\n" + nowInMilisecond + "\n" + postData  

signature = base64.b64encode(hmac.new(secret, stringToSign.encode('utf-8'), digestmod=hashlib.sha512).digest())

return request('post', key, signature, nowInMilisecond, path, postData) 

class BTCMarkets:

def __init__(self, key, secret):
    self.key = key
    self.secret = base64.b64decode(secret)

def trade_history(self, currency, instrument, limit, since):

    data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)])
    postData = json.dumps(data, separators=(',', ':'))
    return post_request(self.key, self.secret, '/order/trade/history', postData) 

def order_create(self, currency, instrument, price, volume, side, order_type, client_request_id):

    data = OrderedDict([('currency', currency),('instrument', instrument),
        ('price', price),('volume', volume),('orderSide', side),('ordertype', order_type),
        ('clientRequestId', client_request_id)])
    postData = json.dumps(data, separators=(',', ':'))
    return post_request(self.key, self.secret, '/order/create', postData) 

def order_history(self, currency, instrument, limit, since):

    data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)])
    postData = json.dumps(data, separators=(',', ':'))
    return post_request(self.key, self.secret, '/order/history', postData) 

def order_open(self, currency, instrument, limit, since):

    data = OrderedDict([('currency', currency),('instrument', instrument),('limit', limit),('since', since)])
    postData = json.dumps(data, separators=(',', ':'))
    return post_request(self.key, self.secret, '/order/open', postData) 

def order_detail(self, order_ids):

    data_obj = {'orderIds':order_ids} 
    postData = json.dumps(data_obj, separators=(',', ':'))
    return post_request(self.key, self.secret, '/order/detail', postData) 

def account_balance(self):

    return get_request(self.key, self.secret, '/account/balance') 

def get_market_tick(self,currency_in,currency_out):

    return get_request(self.key, self.secret, '/market/%s/%s/tick' % (currency_in,currency_out))

def get_market_orderbook(self,currency_in,currency_out):

    return get_request(self.key, self.secret, '/market/%s/%s/orderbook' % (currency_in,currency_out))

def get_market_trades(self,currency_in,currency_out):

    return get_request(self.key, self.secret, '/market/%s/%s/trades' % (currency_in,currency_out))
robmcd commented 6 years ago

Unless I've missed it, you haven't actually told us what request you were trying to send?

Tallthunder commented 6 years ago

Hey @robmcd Sorry I had moved the issue over to the general API section and mentioned it there.

I am simply trying to use get account balance

client = BTCMarkets (api_key, private_key)

print (client.account_balance())