Hash_hmac sha512 authentication in python

I am trying to write python authentication bot for: https://comkort.com/page/private_api

There is no complete php example. I am guessing someone might put it here.

There is only php code snippet:

$query_string = http_build_query($_POST, '', '&');
$ethalon_sign = hash_hmac("sha512", $query_string, $api_secret_key);

      

How to write authentication in python with hash_hmac sha512?

I want to remove mine open orders

POST https://api.comkort.com/v1/private/order/list

.

My current option:

import hashlib
import hmac
import requests
import time

privateKey = b'myprivatekey'
publicKey = 'my public key'

url = 'https://api.comkort.com/v1/private/order/list'
tosign = b'market_alias=doge_ltc'
signing = hmac.new( privateKey , tosign, hashlib.sha512 )
headers = {'sign': signing.digest(), "apikey": publicKey, "nonce": int( time.time() ) }

r = requests.get(url, headers=headers)
print r.text

      

I caught it

{"code":401,"error":"Invalid sign","field":"sign"}

      

Maybe hexdigest () instead of digest ()? I don't know, I am playing around with this prefix b

and the different hmac outputs, every time I catch one error: "Invalid character".

Related: HMAC Signature Requests in Python

+2


source to share


1 answer


If anyone is interesting, I solved it myself.



#!/usr/bin/python

import hashlib
import hmac
import requests
import time

apikey = '';
apisecret = '';

def request_comkort( url, payload ):
        tosign = "&".join( [i + '=' + payload[i] for i in payload] )
        sign = hmac.new( apisecret, tosign , hashlib.sha512);
        headers = {'sign': sign.hexdigest(), 'nonce': int( time.time() ), 'apikey': apikey }
        r = requests.post(url, data=payload, headers=headers)
        return r.text

# Get balance
print request_comkort( "https://api.comkort.com/v1/private/user/balance";, {} )
# Get Open Orders 
print request_comkort( "https://api.comkort.com/v1/private/order/list";, {'market_alias': "DOGE_LTC" } )
# Make Ask
print request_comkort( "https://api.comkort.com/v1/private/order/sell";, { 'market_alias':"HTML_DOGE", "amount": "1000", "price": "123123" } )
# Cancel order
print request_comkort( "https://api.comkort.com/v1/private/order/cancel";, { 'order_id': 10943 } )

      

+9


source







All Articles