本頁按 OAuth 2.0 實際接入流程 重新整理,用於新接入用戶快速走通。
提示: 優先使用 SDK,接入更簡單:https://open.longbridge.com/sdk
API 須知
| 注意事項 | 參考文檔 |
|---|---|
| 推薦使用各自語言的 SDK,而不是調用原生的接口 | SDK 快速開始頁面 |
| 閱讀 OpenAPI 介紹中開通相應服務 | OpenAPI 如何開通 |
| 閱讀 OpenAPI 介紹中使用權限及限制 | OpenAPI 使用權限及限制 |
| 瞭解通用錯誤碼,便於查找調用接口出錯的原因 | 通用錯誤碼 |
OAuth 2.0(推薦方案)
新接入默認使用 OAuth 2.0。API Key 簽名方式作爲備選兼容方案可保留(例如在 SDK/歷史實現中),但不作爲默認接入方式。
Discovery 地址
- 生產環境:
https://openapi.longbridge.com/.well-known/oauth-authorization-server - 中國內地:
https://openapi.longbridge.cn/.well-known/oauth-authorization-server
支持授權類型(以 Discovery 返回爲準):
authorization_coderefresh_token
OAuth 2.0 授權流程
註冊 OAuth 客戶端
如果沒有可視化後台入口,可通過接口動態註冊:
curl -X POST https://openapi.longbridge.com/oauth2/register \
-H "Content-Type: application/json" \
-d '{
"client_name": "my-openapi-app",
"redirect_uris": ["https://your-app.com/callback"],
"grant_types": ["authorization_code", "refresh_token"],
"response_types": ["code"]
}'
註冊返回可能僅包含
client_id(public client,不返回client_secret)。這種情況下請使用 PKCE,並在 token 請求裏不傳client_secret。
構造授權鏈接並獲取 code
https://openapi.longbridge.com/oauth2/authorize
?response_type=code
&client_id=YOUR_CLIENT_ID
&redirect_uri=YOUR_REDIRECT_URI
&scope=3
&state=YOUR_RANDOM_STATE
&code_challenge=YOUR_CODE_CHALLENGE
&code_challenge_method=S256
用戶授權後,回調地址會收到:
YOUR_REDIRECT_URI?code=AUTH_CODE&state=YOUR_RANDOM_STATE
用 code 換 access_token
curl -X POST https://openapi.longbridge.com/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "client_id=YOUR_CLIENT_ID" \
-d "redirect_uri=YOUR_REDIRECT_URI" \
-d "code=AUTH_CODE" \
-d "code_verifier=YOUR_CODE_VERIFIER"
# 僅當客戶端有 secret 時再加:
# -d "client_secret=YOUR_CLIENT_SECRET"
用 Bearer token 調 API(TSLA.US 實例)
curl -X GET "https://openapi.longbridge.com/v1/quote/get_security_list?market=US&category=Overnight" \
-H "Authorization: Bearer ACCESS_TOKEN"
實際返回(節選,保留 TSLA.US 項):
{
"code": 0,
"message": "success",
"data": {
"list": [
{
"symbol": "TSLA.US",
"name_cn": "特斯拉",
"name_hk": "",
"name_en": ""
}
]
}
}
刷新 token
通過 OAuth token endpoint 刷新(詳見下方"刷新 Token"部分):
curl -X POST https://openapi.longbridge.com/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "client_id=YOUR_CLIENT_ID" \
-d "refresh_token=REFRESH_TOKEN"
# 僅當客戶端有 secret 時再加:
# -d "client_secret=YOUR_CLIENT_SECRET"
與舊文檔的關係
- 本頁:只講 OAuth 2.0 主流程(新接入默認看這裏)。
- 下方"刷新 Token"部分:只講刷新步驟細節與常見問題,避免重複。
刷新 Token(OAuth 2.0)
本頁僅說明 OAuth 2.0 的 refresh token 刷新步驟。
- 如果你還沒走完完整授權流程,請先看上方認證部分。
- 本頁不重複註冊 client / 獲取 code 的流程,只關注"刷新"這一步
推薦刷新方式(OAuth 2.0)
使用 OAuth token endpoint:
POST https://openapi.longbridge.com/oauth2/token- 或中國內地:
POST https://openapi.longbridge.cn/oauth2/token
請求參數(application/x-www-form-urlencoded)
| 名稱 | 必須 | 說明 |
|---|---|---|
| grant_type | 是 | 固定爲 refresh_token |
| client_id | 是 | OAuth client id |
| refresh_token | 是 | 上一次簽發的 refresh token |
| client_secret | 否 | 僅機密客戶端需要;public client 不傳 |
刷新示例
curl -X POST https://openapi.longbridge.com/oauth2/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=refresh_token" \
-d "client_id=YOUR_CLIENT_ID" \
-d "refresh_token=YOUR_REFRESH_TOKEN"
# 僅當你的客戶端有 secret 時再加:
# -d "client_secret=YOUR_CLIENT_SECRET"
響應示例
{
"access_token": "...",
"refresh_token": "...",
"expires_in": 2592000,
"token_type": "Bearer"
}
兼容說明
歷史接口 /v1/token/refresh 屬於舊方案兼容路徑,不建議新接入繼續採用。新接入請統一使用 OAuth 2.0 token endpoint。