[英]bingx api How to get user balance with python
這是我為從 BingX API 獲取用戶余額而編寫的代碼。
我認為我做的一切都是正確的,但它不能正常工作。
import urllib.request
import json
import base64
import hmac
import time
APIURL = "https://open-api.bingx.com"
APIKEY = "MyApiKey"
SECRETKEY = "MySecretKey"
def genSignature(paramsStr):
return hmac.new(SECRETKEY.encode("utf-8"),
paramsStr.encode("utf-8"), digestmod="sha256").digest()
def post(url, body):
req = urllib.request.Request(url, headers={
'User-Agent': 'Mozilla/5.0',
'X-BX-APIKEY': APIKEY,
}, method="GET")
return urllib.request.urlopen(req).read()
def getBalance():
paramsMap = {
"timestamp": int(time.time()*1000)
}
paramsStr = "&".join(["%s=%s" % (k, paramsMap[k]) for k in paramsMap])
paramsStr += "&signature=" + genSignature(paramsStr).hex()
url = "%s/openApi/swap/v2/user/balance?%s" % (APIURL, paramsStr)
return post(url, paramsStr)
def main():
print(getBalance())
if __name__ == "__main__":
main()
但是當我運行它時,我得到了這個:
b'{"code":100001,"msg":"","success":false,"timestamp":1675069039381}'
這是文檔鏈接
來自 API 的響應表明請求不成功並返回代碼 100001,成功值為 false。 這意味着發出的請求中存在某種簽名身份驗證錯誤。
100001錯誤碼表示簽名認證失敗。 簽名是用來驗證請求的真實性的,所以如果簽名不正確,請求就會失敗。
有幾件事可能導致簽名失敗:
簽名計算錯誤:確保生成簽名的代碼正確,符合BingX API的要求。
編碼不正確:確保簽名在作為查詢參數添加到請求之前已正確編碼。
密鑰不正確:確保用於生成簽名的密鑰正確且是最新的。
時間戳不正確:確保請求中包含的時間戳正確且格式正確。
您應該仔細查看代碼和 API 文檔,以確保正確生成簽名並且請求中包含所有必需的信息。 如果問題仍然存在,您可能還需要聯系 BingX API 支持團隊以獲得更多幫助。
import requests
# Replace YOUR_API_KEY with your actual Binance API key
headers = {'X-MBX-APIKEY': 'YOUR_API_KEY'}
# Make a GET request to the Binance account endpoint
response = requests.get('https://api.binance.com/api/v3/account', headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Get the user's available balance for the specified asset
asset_balance = [balance for balance in data['balances'] if balance['asset'] == 'BTC'][0]['free']
print('User balance:', asset_balance)
else:
# Handle the error
print('Error:', response.text)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.