当使用Bittrex v3 API时,如果出现“INVALID SIGNATURE”响应,这意味着你的签名无效。这可能是由于以下几个原因引起的:
下面是一个解决这个问题的Python代码示例:
import requests
import hmac
import hashlib
import time
# Bittrex API请求URL
url = "https://api.bittrex.com/v3/orders"
# API密钥和密钥/密钥对
api_key = "YOUR_API_KEY"
api_secret = "YOUR_API_SECRET"
# 请求参数和有效负载
payload = {
"marketSymbol": "BTC-USDT",
"direction": "BUY",
"type": "LIMIT",
"timeInForce": "GOOD_TIL_CANCELLED",
"quantity": "0.001",
"limit": "60000",
"ceiling": None,
"limitPrice": "50000",
"stopPrice": None,
"timeInForce": "GOOD_TIL_CANCELLED",
"clientOrderId": None,
"useAwards": None
}
# 生成时间戳
timestamp = str(int(time.time() * 1000))
# 生成预签名字符串
pre_sign = timestamp + url + "POST"
# 生成签名
signature = hmac.new(api_secret.encode(), pre_sign.encode(), hashlib.sha512).hexdigest()
# 设置请求头
headers = {
"Content-Type": "application/json",
"Api-Key": api_key,
"Api-Timestamp": timestamp,
"Api-Content-Hash": hashlib.sha512(str(payload).encode()).hexdigest(),
"Api-Signature": signature
}
# 发送请求
response = requests.post(url, headers=headers, json=payload)
# 打印响应
print(response.json())
确保你将YOUR_API_KEY
和YOUR_API_SECRET
替换为你的实际API密钥和密钥/密钥对。此示例代码使用了POST请求,如果你使用其他请求方法(如GET),请相应地更改代码。
这个示例代码生成了正确的签名,并将其包含在请求头中,以确保请求的有效性。