Python aiohttp调用Bitunix API下单触发10007签名错误求助
Bitunix API 下单时签名错误(Code 10007)求助
我正在用Python的aiohttp客户端对接Bitunix期货API,已按照官方文档实现认证与签名生成逻辑,但每次下单都返回签名错误:
{ "code": 10007, "data": null, "msg": "Signature Error" }
相关代码
Bitunix客户端实现
import aiohttp, hashlib, uuid, time, json import logging logger = logging.getLogger(__name__) logger.setLevel(logging.DEBUG) class BitunixClient: def __init__(self, api_key, secret_key, base_url="https://fapi.bitunix.com"): self.api_key = api_key self.secret_key = secret_key self.base_url = base_url self.session = aiohttp.ClientSession() async def place_order(self, order_data): method = "POST" endpoint = "/api/v1/futures/trade/place_order" url = f"{self.base_url}{endpoint}" nonce = uuid.uuid4().hex[:32] timestamp = str(int(time.time() * 1000)) # Signature step 1: sorted params (none here) query_params = "" # Signature step 2: sorted, no-whitespace body body = json.dumps(order_data, separators=(',', ':'), sort_keys=True) # Signature step 3: double SHA256 digest_input = f"{nonce}{timestamp}{self.api_key}{query_params}{body}" digest = hashlib.sha256(digest_input.encode()).hexdigest() sign_input = digest + self.secret_key sign = hashlib.sha256(sign_input.encode()).hexdigest() headers = { "api-key": self.api_key, "nonce": nonce, "timestamp": timestamp, "sign": sign, "Content-Type": "application/json" } logger.debug(f"Request URL: {url}") logger.debug(f"Headers: {headers}") logger.debug(f"Body: {body}") logger.debug(f"Digest Input: {digest_input}") logger.debug(f"Signature: {sign}") async with self.session.request(method, url, data=body, headers=headers) as resp: resp_json = await resp.json() logger.info(f"Response: {resp_json}") return resp_json
测试用例
import asyncio async def test_order(): client = BitunixClient("YOUR_API_KEY", "YOUR_SECRET_KEY") order_data = { "symbol": "BTCUSDT", "side": "BUY", "orderType": "LIMIT", "qty": "0.001", "price": "50000", "tradeSide": "OPEN", "effect": "GTC" } result = await client.place_order(order_data) print(result) asyncio.run(test_order())
已尝试的排查步骤
- 反复核对api_key与secret_key的正确性
- 确保请求体已按key排序并压缩(使用
sort_keys=True) - 尝试将请求参数从
json=data切换为data=body - 同步本地系统时间,确保timestamp无偏差
日志片段
Request URL: https://fapi.bitunix.com/api/v1/futures/trade/place_order Headers: {'api-key': '...', 'nonce': '...', 'timestamp': '...', 'sign': '...', 'Content-Type': 'application/json'} Body: {"effect":"GTC","orderType":"LIMIT","price":"50000","qty":"0.001","side":"BUY","symbol":"BTCUSDT","tradeSide":"OPEN"} Signature: ... Response: {'code': 10007, 'data': None, 'msg': 'Signature Error'}
疑问
- 签名生成过程中是否存在遗漏步骤?
- 请求体或请求头是否有官方文档未提及的特殊要求?
- 有没有开发者成功用aiohttp调用Bitunix期货API完成下单?
- 是否有可能是交易所API本身的问题?
内容的提问来源于stack exchange,提问作者Milad
相关产品推荐
相关产品推荐

