Bitcoin Forum
June 24, 2024, 03:31:31 PM *
News: Voting for pizza day contest
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: KRAKEN WEB API via Python  (Read 125 times)
neuri (OP)
Newbie
*
Offline Offline

Activity: 8
Merit: 2


View Profile
August 03, 2020, 06:55:29 PM
 #1

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...

Code:
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
DougM
Full Member
***
Offline Offline

Activity: 173
Merit: 120


View Profile
August 03, 2020, 10:52:45 PM
 #2

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-

HCP
Legendary
*
Offline Offline

Activity: 2086
Merit: 4316

<insert witty quote here>


View Profile
August 03, 2020, 11:03:05 PM
Merited by DougM (1)
 #3

Did you read the Kraken API docs? Huh They have different POST data and header requirements to Bitstamp.

Private methods must use POST and be set up as follows:

HTTP header:
Code:
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:
Code:
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, you can see that the headers and POST data are implemented in code as:
Code: (https://github.com/veox/python2-krakenex/blob/master/krakenex/api.py#L100-L122)
    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

█████████████████████████
████▐██▄█████████████████
████▐██████▄▄▄███████████
████▐████▄█████▄▄████████
████▐█████▀▀▀▀▀███▄██████
████▐███▀████████████████
████▐█████████▄█████▌████
████▐██▌█████▀██████▌████
████▐██████████▀████▌████
█████▀███▄█████▄███▀█████
███████▀█████████▀███████
██████████▀███▀██████████
█████████████████████████
.
BC.GAME
▄▄░░░▄▀▀▄████████
▄▄▄
██████████████
█████░░▄▄▄▄████████
▄▄▄▄▄▄▄▄▄██▄██████▄▄▄▄████
▄███▄█▄▄██████████▄████▄████
███████████████████████████▀███
▀████▄██▄██▄░░░░▄████████████
▀▀▀█████▄▄▄███████████▀██
███████████████████▀██
███████████████████▄██
▄███████████████████▄██
█████████████████████▀██
██████████████████████▄
.
..CASINO....SPORTS....RACING..
█░░░░░░█░░░░░░█
▀███▀░░▀███▀░░▀███▀
▀░▀░░░░▀░▀░░░░▀░▀
░░░░░░░░░░░░
▀██████████
░░░░░███░░░░
░░█░░░███▄█░░░
░░██▌░░███░▀░░██▌
░█░██░░███░░░█░██
░█▀▀▀█▌░███░░█▀▀▀█▌
▄█▄░░░██▄███▄█▄░░▄██▄
▄███▄
░░░░▀██▄▀


▄▄████▄▄
▄███▀▀███▄
██████████
▀███▄░▄██▀
▄▄████▄▄░▀█▀▄██▀▄▄████▄▄
▄███▀▀▀████▄▄██▀▄███▀▀███▄
███████▄▄▀▀████▄▄▀▀███████
▀███▄▄███▀░░░▀▀████▄▄▄███▀
▀▀████▀▀████████▀▀████▀▀
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!