gpt4 book ai didi

python - 使用 SHA56 和 Python 请求的 Binance API 调用

转载 作者:行者123 更新时间:2023-11-28 22:20:57 25 4
gpt4 key购买 nike

没有在 Python 中工作太多,我显然没有发送所要求的正确签名。我如何对其进行哈希处理并正确传递?

SIGNED endpoints require an additional parameter, signature, to be sent in the query string or request body.
Endpoints use HMAC SHA256 signatures. The HMAC SHA256 signature is a keyed HMAC SHA256 operation. Use your secretKey as the key and totalParams as the value for the HMAC operation.
The signature is not case sensitive.
totalParams is defined as the query string concatenated with the request body.

完整文档: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md

import requests, json, time, hashlib


apikey = "myactualapikey"
secret = "myrealsecret"
test = requests.get("https://api.binance.com/api/v1/ping")
servertime = requests.get("https://api.binance.com/api/v1/time")

servertimeobject = json.loads(servertime.text)
servertimeint = servertimeobject['serverTime']

hashedsig = hashlib.sha256(secret)

userdata = requests.get("https://api.binance.com/api/v3/account",
params = {
"signature" : hashedsig,
"timestamp" : servertimeint,
},
headers = {
"X-MBX-APIKEY" : apikey,
}
)
print(userdata)

我得到了

{"code":-1100,"msg":"Illegal characters found in parameter 'signature'; legal range is '^[A-Fa-f0-9]{64}$'."}

最佳答案

这个:

hashedsig = hashlib.sha256(secret)

给你一个散列对象,而不是一个字符串。您需要以十六进制形式获取字符串:

hashedsig = hashlib.sha256(secret).hexdigest()

您可以通过将您链接的文档(显示它们需要十六进制字符串)与您的原始 hashedsig 及其提供的功能进行比较来解决这个问题。

其次,正如评论者所指出的,您需要应用 HMAC,而不仅仅是 SHA256:

params = urlencode({
"signature" : hashedsig,
"timestamp" : servertimeint,
})
hashedsig = hmac.new(secret.encode(), params.encode(), hashlib.sha256).hexdigest()

您可以在这里找到类似的代码:http://python-binance.readthedocs.io/en/latest/_modules/binance/client.html

关于python - 使用 SHA56 和 Python 请求的 Binance API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48592450/

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