SecondMe API

错误码参考

SecondMe API 可能返回的所有错误码列表

本文档列出 SecondMe API 可能返回的所有错误码。

错误响应格式

所有 API 错误都遵循统一的响应格式:

{
  "code": 400,
  "message": "错误描述",
  "error_code": "module.resource.reason"
}
字段类型说明
codenumberHTTP 状态码
messagestring人类可读的错误描述
error_codestring机器可读的错误码

错误码命名规范

错误码遵循 {module}.{resource}.{reason} 格式:

  • module: 模块名称(oauth2、apikey、secondme 等)
  • resource: 资源类型(token、code、session 等)
  • reason: 错误原因(invalid、expired、not_found 等)

通用错误码

错误码HTTP 状态码说明
resource.fetch.not_found404资源不存在
resource.auth.unauthorized401未授权访问

API Key 错误码

错误码HTTP 状态码说明
apikey.fetch.not_found404API Key 不存在
apikey.auth.missing401缺少 Authorization 头
apikey.auth.invalid401API Key 无效或已过期
apikey.permission.denied403缺少必需的权限
apikey.scope.invalid400无效的权限范围

OAuth2 错误码

应用相关

错误码HTTP 状态码说明
oauth2.application.not_found404应用不存在
oauth2.application.unauthorized403无权访问该应用
oauth2.application.invalid_type400应用类型不匹配
oauth2.application.invalid_status400应用状态无效
oauth2.application.pending_review403应用待审核
oauth2.application.rejected403应用已被拒绝
oauth2.application.suspended403应用已被暂停

授权相关

错误码HTTP 状态码说明
oauth2.authorization.not_found404授权记录不存在
oauth2.authorization.revoked401授权已被撤销

令牌相关

错误码HTTP 状态码说明
oauth2.token.invalid401令牌无效
oauth2.token.expired401令牌已过期
oauth2.token.revoked401令牌已被撤销
oauth2.token.not_found404令牌不存在

权限相关

错误码HTTP 状态码说明
oauth2.scope.invalid400无效的权限
oauth2.scope.disallowed403应用不允许请求该权限
oauth2.scope.insufficient403权限不足

客户端凭证相关

错误码HTTP 状态码说明
oauth2.client.invalid400无效的客户端
oauth2.client.secret_mismatch401Client Secret 不匹配

授权码相关

错误码HTTP 状态码说明
oauth2.code.invalid400授权码无效
oauth2.code.expired400授权码已过期
oauth2.code.used400授权码已被使用
oauth2.code.revoked400授权码已被撤销

Redirect URI 相关

错误码HTTP 状态码说明
oauth2.redirect_uri.invalid400无效的 Redirect URI
oauth2.redirect_uri.mismatch400Redirect URI 不匹配

Grant Type 相关

错误码HTTP 状态码说明
oauth2.grant_type.invalid400无效的 grant_type
oauth2.grant_type.unsupported400不支持的 grant_type

Refresh Token 相关

错误码HTTP 状态码说明
oauth2.refresh_token.invalid400Refresh Token 无效
oauth2.refresh_token.expired401Refresh Token 已过期
oauth2.refresh_token.revoked401Refresh Token 已被撤销

SecondMe 错误码

错误码HTTP 状态码说明
secondme.user.invalid_id400无效的用户 ID 格式
secondme.session.not_found404会话不存在
secondme.session.unauthorized403无权访问该会话
secondme.stream.error500流式响应错误
secondme.context.build_failed500上下文构建失败

系统错误码

错误码HTTP 状态码说明
internal.error500内部服务器错误
connection.error503服务连接错误
invalid.param400无效的请求参数

错误处理最佳实践

1. 检查 HTTP 状态码

首先检查 HTTP 状态码判断请求是否成功:

  • 2xx: 请求成功
  • 4xx: 客户端错误(参数错误、权限不足等)
  • 5xx: 服务端错误

2. 解析 error_code

使用 error_code 进行程序化错误处理:

response = api_call()

if response.get("error_code") == "oauth2.token.expired":
    # 刷新 token
    refresh_token()
elif response.get("error_code") == "apikey.permission.denied":
    # 提示用户权限不足
    show_permission_error()

3. 显示 message 给用户

message 字段包含人类可读的错误描述,可直接展示给用户。

4. 重试策略

对于 5xx 错误,建议实现指数退避重试:

import time

def api_call_with_retry(max_retries=3):
    for attempt in range(max_retries):
        response = api_call()
        if response.status_code < 500:
            return response
        time.sleep(2 ** attempt)  # 1, 2, 4 秒
    raise Exception("Max retries exceeded")