【发布时间】:2019-06-24 02:55:24
【问题描述】:
我尝试制作一个简单的函数,向 Kraken 交换 API 发出 HTTP 请求。该方法是私有的,我正在尝试获取我的帐户余额。
根据 Kraken 文档 (https://www.kraken.com/features/api#general-usage):
HTTP 标头:
API-Key = API 密钥
API-Sign = 使用 HMAC-SHA512 of (URI path + SHA256(nonce + POST data)) 和 base64 解码秘密 API 的消息签名 键
POST 数据:
nonce = 总是递增的无符号 64 位整数
otp = 双因素密码(如果启用双因素,否则不需要)
我尝试使我的签名生成类似于“veox”Python 库(可在:https://github.com/veox/python3-krakenex/blob/master/krakenex/api.py 获得)。
我在 Ubuntu 18.04 上使用 Python 3.6.7。
2FA (otp) 已在 Kraken 交易所为我的帐户开启,但我不确定是否需要包含在请求中。
我在堆栈溢出中搜索了解决方案,但我似乎无法从可用的帖子中获得任何信息。 (请记住,我对 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)
【问题讨论】:
标签: python-3.x kraken.com