当使用币安交易所的API时,若出现错误代码-1131并提示"recvWindow必须小于60000",这意味着你设置的接收窗口(recvWindow)超过了最大值。
解决这个问题的方法是将接收窗口的值设置为小于60000。下面是一个示例代码,展示了如何设置recvWindow的值:
import requests
import time
import hashlib
import hmac
# 设置API密钥和密钥
api_key = 'YOUR_API_KEY'
api_secret = 'YOUR_API_SECRET'
# 设置接收窗口的值,小于60000
recvWindow = 5000
# 创建时间戳
timestamp = int(time.time() * 1000)
# 构建签名字符串
query_string = f'recvWindow={recvWindow}×tamp={timestamp}'
signature = hmac.new(api_secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256).hexdigest()
# 构建请求头
headers = {
'X-MBX-APIKEY': api_key
}
# 构建请求参数
params = {
'recvWindow': recvWindow,
'timestamp': timestamp,
'signature': signature
}
# 发送请求
response = requests.get('https://api.binance.com/api/v3/your-endpoint', headers=headers, params=params)
在上面的示例代码中,我们将recvWindow设置为5000,但你可以根据你的需求将其设置为小于60000的任何值。确保将你的API密钥(api_key)和密钥(api_secret)替换为你自己的值,并将'your-endpoint'替换为你要调用的特定API端点。
请注意,这只是一个示例代码,你需要根据你自己的应用程序和需求进行相应的修改。