-
Notifications
You must be signed in to change notification settings - Fork 56
Closed
Description
问题描述
你好,
我今日在行情商店购买了一年的美股期权行情 OpenAPI, 并尝试按如下代码进行访问测试,但结果显示我仍旧无法访问期权数据。
我购买的订单如下:
订单号:Q20260213025826123335670027
我尝试过重置 ACCESS_TOKEN,但是没有效果。
代码例子
配置文件如下:
longport_config.env
# Longbridge API Credentials
LONGPORT_APP_KEY="dd27************be21"
LONGPORT_APP_SECRET="242c2*******e206"
LONGPORT_ACCESS_TOKEN="m**************abc"
运行代码如下:
from longport.openapi import Config, QuoteContext, TradeContext
from datetime import datetime
# 加载配置
config_path = "longport_config.env"
creds = {}
with open(config_path, "r") as f:
for line in f:
line = line.strip()
if "=" in line and not line.startswith("#"):
key, val = line.split("=", 1)
creds[key.strip()] = val.strip().strip('"')
print("="*70)
print("🔍 期权数据权限详细诊断")
print("="*70)
print("\n1️⃣ API Token 信息:")
print(f" App Key: {creds.get('LONGPORT_APP_KEY', 'N/A')[:20]}...")
print(f" Token: {creds.get('LONGPORT_ACCESS_TOKEN', 'N/A')[:30]}...")
print(f" Token 长度: {len(creds.get('LONGPORT_ACCESS_TOKEN', ''))} 字符")
# 重新创建配置(使用新 Token)
print("\n2️⃣ 创建新的 SDK 连接:")
try:
config = Config(
app_key=creds.get("LONGPORT_APP_KEY"),
app_secret=creds.get("LONGPORT_APP_SECRET"),
access_token=creds.get("LONGPORT_ACCESS_TOKEN")
)
print(" ✅ 配置创建成功")
# 测试 Quote 连接
quote_ctx = QuoteContext(config)
print(" ✅ Quote 连接成功")
# 测试 Trade 连接
trade_ctx = TradeContext(config)
print(" ✅ Trade 连接成功")
except Exception as e:
print(f" ❌ 连接失败: {e}")
exit(1)
print("\n3️⃣ Quote Level 详细信息:")
try:
level = quote_ctx.quote_level()
print(f" 完整 Level:\n {level}")
# 解析 US 权限
if "USOption" in level or "Option" in level:
print("\n ✅ 检测到期权权限标识")
else:
print("\n ❌ 未检测到期权权限标识")
except Exception as e:
print(f" ❌ 获取失败: {e}")输出结果见下方截图;
错误信息或结果截图
输出结果如下, 红框中显示无法访问期权数据:
你的环境信息
- 操作系统: Debian 12
- 开发语言: Python
- SDK 版本号:使用 pip show longport 显示如下结果:
Name: longport
Version: 3.0.18
Summary: A Python library for LongPort Open API
Home-page: https://open.longportapp.com/en/
Author:
Author-email:
License:Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
