签名不匹配 - POST 对 BingX API 的 HTTP 请求与 Python

Signature does not match - POST HTTP Request to BingX API with Python

我正在尝试通过 Python 中的 post 请求与交易平台的 API 进行通信。不幸的是,这仅在不必签署请求时才有效。一开始我只是想按照文档(BingX API Documentation on GitHub)的例子来获取账户余额。这个例子给我的印象是这需要半个小时,但现在我已经做了两天,我慢慢开始绝望了。

当我发送请求时,我从服务器收到一条错误消息,指出我的签名不正确:

{"code":80014,"msg":"signature not match","data":{}}

由于我没有加密或类似的经验,所以我很难分析错误。我可以想象错误在于转换为字节,但我也不能为了测试目的而省略这一步。文档要求你按照这个方案加密字符串:

Signature = HmacSHA256("UuGuyEGt6ZEkpUObCYCmIfh0elYsZVh80jlYwpJuRZEw70t6vomMH7Sjmf94ztSI", "POST/api/v1/user/getBalanceapiKey=Zsm4DcrHBTewmVaElrdwA67PmivPv6VDK6JAkiECZ9QfcUnmn67qjCOgvRuZVOzU&currency=USDT&timestamp=1615272721001")
Signature = Base64Encode(Signature)
Signature = UrlEncode(Signature)

我“解决”如下:

signature       =   hmac.new(api_secret.encode('utf-8'), originstring.encode('utf-8'), hashlib.sha256).digest().upper()
signature       =   str(signature)
signature       =   bytes(signature, 'utf-8')
signature       =   base64.b64encode(signature)
signature       =   urllib.parse.quote(signature)

如果有人能向我解释我做错了什么,我会很高兴。

非常感谢

丹尼尔

我的完整Python代码:

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

api_key         =   "tHeKeY"
api_secret      =   "MySuPeRsEcReT"
asset           =   "USDT"
want            =   "getBalance"

timestamp       =   str(int(time.time()))

paramstring     =   (str("apiKey=")     +
                    str(api_key)        +
                    str("&currency=")   +
                    str(asset)          +
                    str("&timestamp=")  +
                    str(timestamp))

print("PARAMSTRING:")
print(paramstring)
print("")

originstring    =   (str("POST/api/v1/user/") +
                    str(want) +
                    str(paramstring))

print("ORIGINSTRING:")
print(originstring)
print("")

signature       =   hmac.new(api_secret.encode('utf-8'), originstring.encode('utf-8'), hashlib.sha256).digest().upper()
signature       =   str(signature)
signature       =   bytes(signature, 'utf-8')
signature       =   base64.b64encode(signature)
signature       =   urllib.parse.quote(signature)

print("SIGNATURE:")
print(signature)
print("")

signature = str(signature)

requeststring   =   (str("https://api-swap-rest.bingbon.pro/api/v1/user/getBalance?") +
                    str("apiKey=")      +
                    str(api_key)        +
                    str("&currency=")   +
                    str(asset)          +
                    str("&timestamp=")  +
                    str(timestamp)      +
                    str("&sign=")       +
                    str(signature))

print("REQUESTSTRING:")
print(requeststring)
print("")
print("RESPONSE:")

response            =   requests.post(requeststring)
response            =   str(response.text)
print(response)
response            =   json.loads(response)
response_code       =   (response["code"])
response_message    =   (response["msg"])
response_data       =   (response["data"])

print(response_code)
print(response_message)
print(response_data)

无限的猴子和无限的打字机,有足够的时间你可以试验。

我不知道为什么,但是当我这样做时它起作用了:

signature       =   (base64.b64encode(
                hmac.new(bytes(api_secret, 'utf-8'), bytes(originstring, 'utf-8'),digestmod=hashlib.sha256).digest()).decode("utf-8"))

但是无论如何,如果有人能告诉我我的问题出在哪里,那就太好了。因为对我来说这两种解决方案看起来都一样。