5 分钟快速接入
以一个「AI 绘图」Web 应用为例,完成登录 + 扣积分两条主链路。
第 1 步:在后台创建应用
- 登录管理后台 → 应用接入 → 新建应用
- 填写应用名称、类型(Web 应用)、协议(OIDC)
- Redirect URI 填
https://your-app.com/oauth/callback(必须完全一致) - Scope 勾选
openidprofileemailpoints:readpoints:writeusage:write - 保存后立即复制 client_id 与 client_secret
第 2 步:跳转登录(前端)
https://sso.ingu.cn/oauth/authorize
?response_type=code
&client_id=ingu_xxxxxxxxxxxxxxxx
&redirect_uri=https%3A%2F%2Fyour-app.com%2Foauth%2Fcallback
&scope=openid%20profile%20email
&state=随机字符串
&nonce=随机字符串
用户登录并同意授权后,浏览器会跳回:
https://your-app.com/oauth/callback?code=CODE&state=随机字符串
必须校验回调中的
state与发起时一致,防止 CSRF。
第 3 步:换取令牌(服务端)
curl -X POST https://sso.ingu.cn/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d grant_type=authorization_code \
-d client_id=ingu_xxxxxxxxxxxxxxxx \
-d client_secret=sk_xxxxxxxxxxxxxxxx \
-d code=CODE \
-d redirect_uri=https://your-app.com/oauth/callback
响应:
{
"access_token": "3xJ9...kQ",
"token_type": "Bearer",
"expires_in": 7200,
"scope": "openid profile email",
"id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6ImluZ3Utc3NvLTIwMjYifQ...",
"refresh_token": "9aF2...Zx"
}
第 4 步:校验 id_token
- 从
https://sso.ingu.cn/oauth/jwks获取公钥(RS256) - 校验签名,并检查
iss === http://sso.ingu.cn、aud === 你的 client_id、exp未过期、nonce与发起时一致 - 用
sub(用户唯一标识,UUID)作为本地账号主键建立映射
第 5 步:调用积分接口
# 扣减 10 积分(request_id 保证幂等,重试不会重复扣费)
curl -X POST https://sso.ingu.cn/api/v1/points/consume \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"amount": 10,
"request_id": "task_20261124_0001",
"description": "生成 5 张图",
"metric": "image"
}'
响应:
{
"success": true,
"data": {
"txn_no": "OUT261124153012345678",
"amount": 10,
"balance_before": 120.5,
"balance_after": 110.5,
"idempotent": false
},
"request_id": "..."
}
第 6 步:用量上报(推荐)
如果按实际消耗计费,不要直接传积分,而是上报用量,由平台按单价折算:
curl -X POST https://sso.ingu.cn/api/v1/usage/report \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"metric":"image","quantity":5,"model":"kolors-v2","request_id":"task_20261124_0001"}'
接入检查清单
- [ ] Redirect URI 与后台登记完全一致(协议、域名、端口、路径、末尾斜杠)
- [ ]
state、nonce随机生成并校验 - [ ]
client_secret只存在于服务端 - [ ] id_token 校验签名与
aud/iss/exp - [ ] 扣减接口使用业务唯一
request_id,支持失败重试 - [ ] 积分不足(HTTP 402 /
INSUFFICIENT_POINTS)时引导用户到充值页 - [ ] 处理
refresh_token刷新逻辑,避免频繁重新登录