调用Bittrex API V3时签名无效问题求助(Python 3.10)
Bittrex API V3 签名无效问题修复
核心问题排查
你的代码存在两个关键错误导致签名验证失败:
- URI格式错误:签名时必须使用相对路径(如
/v3/balances),而非完整URL(https://api.bittrex.com/v3/balances),Bittrex V3 API的签名规则明确要求使用相对请求路径。 - 签名字符串拼接顺序错误:正确的签名字符串顺序应为
timestamp + HTTP方法 + 请求URI + Content-Hash + 子账户ID,你的代码把HTTP方法放在了URI之后,顺序完全颠倒。
额外注意事项
- 时间同步:Bittrex要求客户端时间与服务器时间差不超过30秒,建议通过
GET /v3/time接口获取服务器时间,避免本地时间偏差导致的签名失败。 - Content-Hash:GET请求且无请求体时,Content-Hash确实是空字符串的SHA-512哈希值,你当前使用的
cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e是正确的。
修正后的代码
import json import requests import hashlib import hmac # 获取Bittrex服务器时间,避免本地时间偏差 def get_bittrex_server_time(): response = requests.get('https://api.bittrex.com/v3/time') return response.json()['serverTime'] Api_Timestamp = str(get_bittrex_server_time()) Api_Key = '你的API密钥' Api_secret = '你的API密钥' # 使用签名要求的相对路径 uri = '/v3/balances' full_url = 'https://api.bittrex.com' + uri Api_Content_hash = 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e' Api_subaccount = '' # 按照正确顺序拼接签名载荷 signature_payload = Api_Timestamp + 'GET' + uri + Api_Content_hash + Api_subaccount # 生成HMAC-SHA512签名并转十六进制 Api_Signature_encoded = hmac.new(Api_secret.encode(), signature_payload.encode(), hashlib.sha512).hexdigest() headers = { 'Accept': 'application/json', 'Api-Key': Api_Key, 'Api-Timestamp': Api_Timestamp, 'Api-Content-Hash': Api_Content_hash, 'Api-Signature': Api_Signature_encoded, } response_orders = requests.get(url=full_url, headers=headers).json() print(response_orders)
代码修改说明
- 新增服务器时间获取函数,规避本地时间与服务器时间差过大的问题。
- 将URI改为签名要求的相对路径,请求时再拼接完整URL。
- 修正签名字符串的拼接顺序,严格匹配Bittrex的规则。
- 移除GET请求中不必要的
Content-Type头,减少冗余。
内容的提问来源于stack exchange,提问作者Molb
相关产品推荐
相关产品推荐

