'Kraken API - Account balances request returning Invalid Nonce
so I am using the kraken api docs and I am trying to return my account balances but when running my python file I am getting {'error': ['EAPI:Invalid nonce']} returned. For my nonce in both signature & request I am using str(int(1000*time.time())).
import time
import os
import requests
# Signiture imports
import urllib.parse
import hashlib
import hmac
import base64
# Create Signture 
def get_kraken_signature(urlpath, data, secret):
    postdata = urllib.parse.urlencode(data)
    encoded = (str(data['nonce']) + postdata).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()
    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    sigdigest = base64.b64encode(mac.digest())
    return sigdigest.decode()
api_sec = os.environ['API_SEC_KRAKEN']
data = {
    "nonce": str(int(1000*time.time())), 
}       
signature = get_kraken_signature("/0/private/AddOrder", data, api_sec)
print("API-Sign: {}".format(signature))
# Account Balances
# Read Kraken API key and secret stored in environment variables
api_url = "https://api.kraken.com"
api_key = os.environ['API_KEY_KRAKEN']
api_sec = os.environ['API_SEC_KRAKEN']
# Attaches auth headers and returns results of a POST request
def kraken_request(uri_path, data, api_key, api_sec):
    headers = {}
    headers['API-Key'] = api_key
    # get_kraken_signature() as defined in the 'Authentication' section
    headers['API-Sign'] = get_kraken_signature(uri_path, data, api_sec)             
    req = requests.post((api_url + uri_path), headers=headers, data=data)
    return req
# Construct the request and print the result
resp = kraken_request('/0/private/Balance', {
    "nonce": str(int(1000*time.time()))
}, api_key, api_sec)
print(resp.json())
I done some research and I know the nonce has to be an increasing value so thought that would work, any help is appreciated thanks :)
Solution 1:[1]
I came across the same problem, the string length of nonce.
I tested my Kraken API in Postman. It worked.
The Postman code for nonce is:
api_nonce = (Date.now() * 1000).toString();
The result is for example:
1650897463009000
The Kraken API Python example code that comes from Kraken generates nonce with this code:
str(int(1000*time.time()))
It returns this:
1650897704461
I changed the code to:
str(1000*int(1000*time.time()))
The new code returns this:
1650897727222000
The API works now, happy coding!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | Janooo | 
