没有在 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

10-08 17:34