title: Kraken API 'EAPI:Invalid key' with Python3
body: <p>I tried making a simple function which makes a HTTP request to Kraken exchange API. The method is private, I am trying to fetch my account balance. </p>
  
  <p>According to Kraken documentation (<a href="https://www.kraken.com/features/api#general-usage" rel="nofollow noreferrer">https://www.kraken.com/features/api#general-usage</a>):</p>
  
  <blockquote>
    <p><strong>HTTP header:</strong></p>
    
    <p>API-Key = API key</p>
    
    <p>API-Sign = Message signature using HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) and base64 decoded secret API
    key</p>
    
    <p><strong>POST data:</strong></p>
    
    <p>nonce = always increasing unsigned 64 bit integer </p>
    
    <p>otp = two-factor password (if two-factor enabled, otherwise not required)</p>
  </blockquote>
  
  <p>I tried to make my signature generation similar to the "veox" Python library (available at: <a href="https://github.com/veox/python3-krakenex/blob/master/krakenex/api.py" rel="nofollow noreferrer">https://github.com/veox/python3-krakenex/blob/master/krakenex/api.py</a>).</p>
  
  <p>I am using Python 3.6.7 on Ubuntu 18.04.</p>
  
  <p>2FA (otp) is turned on for my account on Kraken exchange, though I am not sure if I need to include in the request.</p>
  
  <p>I searched stack overflow for the solution, but I can't seem to get anything from the posts available. (Please keep in mind I'm fairly new to Python and Stack Overflow)</p>
  
  <p>I get 200 response from the server so I am pretty sure the problem is in generating the signature.</p>
  
  <p>Here is my code (xxx, yyy and zzz variables are purposefully written like that):  </p>
  
  <pre><code>Kraken_secret_key = 'xxx' 
  
  Kraken_headers ={
  
      'Kraken_API_key': 'yyy' 
  
  }
  
  def Kraken_account_balance(Kraken_headers):
  
      URI_path= '/0/private/Balance'
  
      URL_path = 'https://api.kraken.com/0/private/Balance'
  
      Kraken_nonce = str(int(time.time()*1000))
  
      otp = 'zzz'
  
      Kraken_POST_data = {
  
          'nonce': Kraken_nonce,
          'otp': str(otp)
  
      } 
  
      encoded = (str(Kraken_nonce)+str(otp)).encode()  
  
      message = URI_path.encode() + hashlib.sha256(encoded).digest() 
  
      Kraken_signature = hmac.new(base64.b64decode(Kraken_secret_key), message, digestmod=hashlib.sha512)
  
      Kraken_signature_digest = base64.b64encode(Kraken_signature.digest())
  
      Kraken_headers['Kraken_API_Signature'] = Kraken_signature_digest.decode()
  
      response = requests.post(URL_path,data= Kraken_POST_data, headers = Kraken_headers)
  
      result = response.json()
  
      print(result)
  </code></pre>