【问题标题】:Binance API call with SHA56 and Python requests使用 SHA56 和 Python 请求的 Binance API 调用
【发布时间】:2018-07-13 12:45:03
【问题描述】:

没有在 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}$'."}

【问题讨论】:

  • 你必须使用hmac

标签: python python-3.x python-requests sha256 sha


【解决方案1】:

这个:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多