gpt4 book ai didi

python-3.x - Kraken API 'EAPI:Invalid key' 与 Python3

转载 作者:行者123 更新时间:2023-12-02 00:23:53 27 4
gpt4 key购买 nike

我尝试制作一个简单的函数,向 Kraken 交换 API 发出 HTTP 请求。该方法是私有(private)的,我正在尝试获取我的帐户余额。

根据 Kraken 文档(https://www.kraken.com/features/api#general-usage):

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)

我试图使我的签名生成类似于“veox”Python 库(可在:https://github.com/veox/python3-krakenex/blob/master/krakenex/api.py 获得)。

我在 Ubuntu 18.04 上使用 Python 3.6.7。

我在 Kraken 交易所的帐户已启用 2FA (otp),但我不确定是否需要在请求中包含。

我在堆栈溢出中搜索了解决方案,但我似乎无法从可用的帖子中获得任何信息。 (请记住,我是 Python 和 Stack Overflow 的新手)

我从服务器收到 200 响应,所以我很确定问题出在生成签名上。

这是我的代码(xxx、yyy 和 zzz 变量是故意这样写的):

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)

最佳答案

所以我弄清楚了为什么我的代码不起作用。

简答:

  1. Kraken API 调用中使用的 POST 数据必须经过 URL 编码。这意味着“nonce”和“otp”必须经过 URL 编码才能使 API 正常工作。我使用“urllib”模块中的“urllib.parse.urlencode”方法让 API 正常工作。
  2. header 值必须明确称为与 Kraken API 手册中相同的名称。

长答案:

  1. 这可能是因为我是 API 编码的初学者,但 Kraken API 手册没有明确说明 POST 数据必须进行 URL 编码。在这种情况下,OTP(2 因素身份验证)不会影响我的代码,因此我在调用中删除了那部分 POST 数据。

在我的例子中,我使用的唯一 POST 数据是“nonce”值。例如,如果在上面的代码中 nonce 等于

'nonce': 666999

调用中使用的相同值,但 URL 编码使用 'urllib.parse.urlencode' 方法将等于

"nonce=666999"
  1. 同样,对于更有经验的开发人员来说,这可能不是问题,但对我来说, header 值必须明确地调用与 Kraken API 手册中相同的名称并不明显。

所以在上面的代码中

Kraken_headers ={

'Kraken_API_key': 'yyy'

}

Kraken_headers['Kraken_API_Signature'] = Kraken_signature_digest.decode() 

应该重命名为

Kraken_headers ={

'API-Key': 'yyy'

}

 Kraken_headers['API-Sign'] = Kraken_signature_digest.decode()

这是完整的工作代码,只需将私钥和公钥值替换为您的值即可:

import requests
import time
import hmac
import hashlib
import json
import base64
import urllib

Kraken_secret_key = 'xxx'

Kraken_headers ={

'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))

Kraken_POST_data = {

'nonce': Kraken_nonce
}

url_encoded_post_data = urllib.parse.urlencode(Kraken_POST_data)

encoded = (str(Kraken_POST_data['nonce'])+url_encoded_post_data).encode()

message = URI_path.encode() + hashlib.sha256(encoded).digest()

Kraken_signature = hmac.new(base64.b64decode(Kraken_secret_key), message,
hashlib.sha512)

Kraken_signature_digest = base64.b64encode(Kraken_signature.digest())

Kraken_headers['API-Sign'] = Kraken_signature_digest.decode()

response = requests.post(URL_path,data= Kraken_POST_data, headers =
Kraken_headers)

result = response.json()

print(result)

Kraken_account_balance(Kraken_headers)

关于python-3.x - Kraken API 'EAPI:Invalid key' 与 Python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54447160/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com