【问题标题】:Binance futures with python, requests and signature带有 python、请求和签名的币安期货
【发布时间】:2021-09-08 05:08:59
【问题描述】:

我正在尝试使用 requests 模块通过 python 交易币安期货。

我有以下代码:

import requests
import time
API_KEY ="your key"
SECRET_KEY="your secret key"

base_url = "https://fapi.binance.com"
api_path = "/fapi/v1/order"

headers={
'X-MBX-APIKEY': API_KEY
}

my_time=int(time.time() * 1000)
my_timestamp="timestamp="+str(my_time)

url_open=base_url+api_path+"?symbol=MATICUSDT&side=BUY&type=MARKET&quantity=40&"+my_timestamp+"&signature="+SECRET_KEY

response = requests.get(url_open,headers=headers)

此代码返回以下错误: {"code":-1022,"msg":"此请求的签名无效。"}

我也试过散列签名:

import hmac
import hashlib
query_string="symbol=MATICUSDT&side=BUY&type=MARKET&quantity=40&"+my_timestamp
HashSig=hmac.new(SECRET_KEY.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()

url_open=base_url+api_path+"?symbol=MATICUSDT&side=BUY&type=MARKET&quantity=40&"+my_timestamp+"&signature="+HashSig

response = requests.get(url_open,headers=headers)

这也返回错误: {"code":-1022,"msg":"此请求的签名无效。"}

我已经尝试了以下代码:

query_string=my_timestamp
HashSig=hmac.new(SECRET_KEY.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()

url_open=base_url+api_path+"?symbol=MATICUSDT&side=BUY&type=MARKET&quantity=40&"+my_timestamp+"&signature="+HashSig

response = requests.get(url_open,headers=headers)

这给出了同样的错误: {"code":-1022,"msg":"此请求的签名无效。"}

我不知道怎么做签名,谁能帮帮我 我在谷歌上搜索了很多,但我一直无法找到解决方案。 我的目标是用 python 交易币安期货

【问题讨论】:

标签: python python-requests hmac binance binance-api-client


【解决方案1】:

首先,我认为您需要发出post 请求而不是get 请求来创建订单。查看 API 文档:https://binance-docs.github.io/apidocs/futures/en/#new-order-trade

Binance API 我也遇到了类似的问题。

这最终对我有用:

from urllib.parse import urlencode
import hmac

params = {'symbol':'MATICUSDT', 
'side':'BUY',
'type':'MARKET',
'quantity':40, 
'timestamp': int(time.time() * 1000) - 3000
}

signature_payload = urlencode(params)
signature = hmac.new(SECRET_KEY.encode(), signature_payload.encode(), 'sha256').hexdigest()
params['signature'] = signature

# pass the params to requests.post
# I took a different approach though

我还必须从时间戳中减去 3000,否则服务器会一直抱怨。

【讨论】:

    猜你喜欢
    • 2021-08-02
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2015-10-07
    • 2016-04-18
    相关资源
    最近更新 更多