使用CCXT库获取OneTrading市场数据返回空字典的原因排查
使用CCXT从OneTrading获取公开数据返回空字典的原因分析
我尝试用Python的CCXT库从加密货币交易所OneTrading获取可用市场这类公开数据,已经创建了带读取权限的API密钥,但CCXT返回空字典。以下是示例代码和请求响应输出:
示例代码
from pprint import pprint import ccxt api_key = "<redacted>" creds = { 'apiKey': api_key, 'verbose': True, # for debug output } exchange = ccxt.onetrading(creds) pprint(exchange.requiredCredentials) # prints required credentials exchange.check_required_credentials() # raises AuthenticationError pprint(exchange.load_markets())
请求响应输出
{'accountId': False, 'apiKey': True, 'login': False, 'password': False, 'privateKey': False, 'secret': False, 'token': False, 'twofa': False, 'uid': False, 'walletAddress': False} fetch Request: onetrading GET https://api.onetrading.com/public/v1/currencies RequestHeaders: {'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'} RequestBody: None fetch Response: onetrading GET https://api.onetrading.com/public/v1/currencies 200 ResponseHeaders: {'Date': 'Sat, 11 May 2024 07:34:44 GMT', 'Content-Type': 'application/json', 'Content-Length': '2', 'Connection': 'keep-alive', 'apigw-requestid': 'XmHfOicWDoEEPOg=', 'strict-transport-security': 'max-age=31536000; includeSubDomains', 'x-frame-options': 'sameorigin', 'x-xss-protection': '1; mode=block', 'x-content-type-options': 'nosniff', 'referrer-policy': 'no-referrer', 'Cache-Control': 'no-store', 'pragma': 'no-cache', 'bp-request-id': '6e914827-43be-4fb8-9b52-cd9d93caadaa', 'CF-Cache-Status': 'DYNAMIC', 'Server': 'cloudflare', 'CF-RAY': '88207cfe9cb49722-AMS'} ResponseBody: [] fetch Request: onetrading GET https://api.onetrading.com/public/v1/instruments RequestHeaders: {'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'} RequestBody: None fetch Response: onetrading GET https://api.onetrading.com/public/v1/instruments 200 ResponseHeaders: {'Date': 'Sat, 11 May 2024 07:34:44 GMT', 'Content-Type': 'application/json', 'Content-Length': '2', 'Connection': 'keep-alive', 'apigw-requestid': 'XmHfQjFgjoEEPFA=', 'strict-transport-security': 'max-age=31536000; includeSubDomains', 'x-frame-options': 'sameorigin', 'x-xss-protection': '1; mode=block', 'x-content-type-options': 'nosniff', 'referrer-policy': 'no-referrer', 'Cache-Control': 'no-store', 'pragma': 'no-cache', 'bp-request-id': '7aee16cb-fc32-47bc-b8c5-60f2d8fa54e1', 'CF-Cache-Status': 'DYNAMIC', 'Server': 'cloudflare', 'CF-RAY': '88207d002e889722-AMS'} ResponseBody: [] {}
原因分析
- 请求未携带API密钥认证:从请求头可以看到,调用公开接口时没有包含API Key相关的认证字段。OneTrading的公开接口可能需要API Key才能返回有效数据,当前CCXT没有将你提供的密钥添加到请求头中,导致接口返回空数组。
- CCXT对OneTrading的适配缺陷:当前版本的CCXT可能没有正确实现OneTrading的认证逻辑,比如没有将API Key放入要求的请求头字段(如
X-API-Key),导致请求未通过权限校验,返回空数据。 - API密钥配置问题:虽然你创建了读取权限的密钥,但可能密钥未激活、权限范围未覆盖公开数据接口,或者OneTrading后台对该密钥的配置存在问题。
解决建议
- 手动测试接口:用curl直接调用接口并携带API Key,验证是否能返回数据:
如果能返回数据,说明是CCXT的适配问题。curl -H "X-API-Key: <你的API密钥>" https://api.onetrading.com/public/v1/instruments - 更新CCXT到最新版本:执行
pip install --upgrade ccxt,看新版本是否修复了OneTrading的接口调用问题。 - 手动添加认证请求头:在代码中手动给exchange添加认证头,再尝试加载市场数据:
exchange = ccxt.onetrading(creds) exchange.headers = {'X-API-Key': api_key} # 根据OneTrading文档确认头字段 pprint(exchange.load_markets())
内容的提问来源于stack exchange,提问作者dovregubben
相关产品推荐
相关产品推荐

