【问题标题】:Signature does not match - POST HTTP Request to BingX API with Python签名不匹配 - 使用 Python 向 BingX API 发布 HTTP 请求
【发布时间】:2022-06-11 13:43:23
【问题描述】:

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

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

{"code":80014,"msg":"签名不匹配","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)

【问题讨论】:

    标签: python post signature hashlib


    【解决方案1】:

    无限的猴子和无限的打字机,有足够的时间可以进行实验。

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

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

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

    【讨论】:

      【解决方案2】:

      你解决问题了吗?我已经按照你的代码,但它说 {"code":80014,"msg":"signature not match","data":{}}。如果你能帮上忙,对我真的很有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        • 1970-01-01
        • 2016-11-03
        • 2018-04-23
        • 2023-04-01
        • 1970-01-01
        • 2012-08-31
        相关资源
        最近更新 更多