Title: KRAKEN WEB API via Python
Post by: neuri on August 03, 2020, 06:55:29 PM
Hello, i'm trying to use the Kraken WEB API via a simple Python Script for placing Orders or getting the actual User Balance without !!! using any Kraken spec. Library. Unfortunately i have no Chance to have success with this. I'm using this method for the Bitstamp API without any Problems... def BuyMarket(curr,amount): client_id = xxx api_key = xxx api_secret = xxx
amount = round(amount, 8)
timestamp = str(int(round(time.time() * 1000))) nonce = str(uuid.uuid4()) content_type = 'application/x-www-form-urlencoded' payload = {'amount': amount}
if sys.version_info.major >= 3: from urllib.parse import urlencode else: from urllib import urlencode
payload_string = urlencode(payload)
message = 'BITSTAMP ' + api_key + \ 'POST' + \ 'www.bitstamp.net' + \ '/api/v2/buy/market/' + curr +'/' + \ '' + \ content_type + \ nonce + \ timestamp + \ 'v2' + \ payload_string message = message.encode('utf-8') signature = hmac.new(API_SECRET, msg=message, digestmod=hashlib.sha256).hexdigest() headers = { 'X-Auth': 'BITSTAMP ' + api_key, 'X-Auth-Signature': signature, 'X-Auth-Nonce': nonce, 'X-Auth-Timestamp': timestamp, 'X-Auth-Version': 'v2', 'Content-Type': content_type } r = requests.post( 'https://www.bitstamp.net/api/v2/buy/market/' + curr +'/', headers=headers, data=payload_string ) if not r.status_code == 200: # raise Exception('Status code not 200') raise Exception(r.status_code) string_to_sign = (nonce + timestamp + r.headers.get('Content-Type')).encode('utf-8') + r.content signature_check = hmac.new(API_SECRET, msg=string_to_sign, digestmod=hashlib.sha256).hexdigest() if not r.headers.get('X-Server-Auth-Signature') == signature_check: raise Exception('Signatures do not match')
log ("Place Market Buy Order - amount:" + str(amount) + " currencypair:"+curr+ " " + str(r.content) )
try: OrderID=json.loads(r.text)["id"] except: OrderID="error"
return(OrderID) Does anyone have an example Python Code who show's me how this would work for KRAKEN (Place Orders or get Balance...) Thank a lot Neuri
Title: Re: KRAKEN WEB API via Python
Post by: DougM on August 03, 2020, 10:52:45 PM
Hello,
i'm trying to use the Kraken WEB API via a simple Python Script for placing Orders or getting the actual User Balance without !!! using any Kraken spec. Library. Unfortunately i have no Chance to have success with this. I have used Python to access other bitcoin, APIs but not Kraken unfortunately. These Kraken python examples were not helpful? https://support.kraken.com/hc/en-us/articles/360025174872-How-to-create-the-krakenapi-py-file https://support.kraken.com/hc/en-us/articles/360025180232-Kraken-REST-API-command-line-client https://support.kraken.com/hc/en-us/articles/218198197-How-to-retrieve-historical-time-and-sales-trading-history-using-the-REST-API-Trades-endpoint-
Title: Re: KRAKEN WEB API via Python
Post by: HCP on August 03, 2020, 11:03:05 PM
Did you read the Kraken API docs (https://www.kraken.com/features/api)? ??? They have different POST data and header requirements to Bitstamp. Private methods must use POST and be set up as follows: HTTP header: API-Key = API key API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API key
POST data: nonce = always increasing unsigned 64 bit integer otp = two-factor password (if two-factor enabled, otherwise not required)
If you look through the github for the Kraken Python 2 example (https://github.com/veox/python2-krakenex), you can see that the headers and POST data are implemented in code as: def query_private(self, method, req={}, conn = None): """API queries that require a valid key/secret pair. Arguments: method -- API method name (string, no default) req -- additional API request parameters (default: {}) conn -- connection object to reuse (default: None) """ urlpath = '/' + self.apiversion + '/private/' + method
req['nonce'] = int(1000*time.time()) postdata = urllib.urlencode(req) message = urlpath + hashlib.sha256(str(req['nonce']) + postdata).digest() signature = hmac.new(base64.b64decode(self.secret), message, hashlib.sha512) headers = { 'API-Key': self.key, 'API-Sign': base64.b64encode(signature.digest()) }
return self._query(urlpath, req, conn, headers)
Full api.py is here: https://github.com/veox/python2-krakenex/blob/master/krakenex/api.py
|