App OAuth 授权接口文档 - 获取授权码和访问令牌
App OAuth 授权接口文档
1. App OAuth 授权接口
接口名称: App OAuth 授权接口
接口描述: 该接口用于实现第三方 App 与用户授权登录的功能,通过 OAuth 协议进行认证,获取用户信息。
请求 URL: https://api.example.com/oauth/authorize
请求方式: GET
请求参数:
| 参数名 | 必选 | 类型 | 说明 | |---|---|---|---| | response_type | 是 | String | 固定值'code' | | client_id | 是 | String | 第三方 App 的 ID | | redirect_uri | 是 | String | 重定向 URI,用于接收授权码 | | state | 否 | String | 用于防止 CSRF 攻击,建议随机生成 |
响应参数:
| 参数名 | 必选 | 类型 | 说明 | |---|---|---|---| | code | 是 | String | 授权码,有效期 10 分钟 | | state | 否 | String | 与请求参数中 state 参数一致 |
错误码:
| 错误码 | 错误信息 | 说明 | |---|---|---| | 400 | invalid_request | 请求参数无效 | | 401 | unauthorized_client | 未授权的客户端 | | 403 | access_denied | 拒绝访问 | | 500 | server_error | 服务器错误 |
请求示例:
https://api.example.com/oauth/authorize?response_type=code&client_id=123456&redirect_uri=http://example.com/callback&state=xyz
响应示例:
http://example.com/callback?code=xxxxxxxxxxxxxxx&state=xyz
2. App OAuth 授权码换取 Token 接口
接口名称: App OAuth 授权码换取 Token 接口
接口描述: 该接口用于实现授权码换取 Token 的功能,获取访问令牌,用于访问用户信息。
请求 URL: https://api.example.com/oauth/token
请求方式: POST
请求参数:
| 参数名 | 必选 | 类型 | 说明 | |---|---|---|---| | grant_type | 是 | String | 固定值'authorization_code' | | code | 是 | String | 授权码 | | redirect_uri | 是 | String | 与授权请求中的重定向 URI 一致 | | client_id | 是 | String | 第三方 App 的 ID | | client_secret | 是 | String | 第三方 App 的 Secret |
响应参数:
| 参数名 | 必选 | 类型 | 说明 | |---|---|---|---| | access_token | 是 | String | 访问令牌,用于访问用户信息 | | token_type | 是 | String | 令牌类型,固定值'Bearer' | | expires_in | 是 | Number | 访问令牌的有效期,单位秒 | | refresh_token | 否 | String | 刷新令牌,用于获取新的访问令牌 | | scope | 否 | String | 授权范围,与授权请求中的 scope 一致 | | openid | 是 | String | 用户 ID,用于唯一标识用户身份 |
错误码:
| 错误码 | 错误信息 | 说明 | |---|---|---| | 400 | invalid_request | 请求参数无效 | | 401 | invalid_client | 客户端验证失败 | | 403 | access_denied | 拒绝访问 | | 500 | server_error | 服务器错误 |
请求示例:
POST /oauth/token HTTP/1.1
Host: api.example.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=xxxxxxxxxxxxxxx&redirect_uri=http://example.com/callback&client_id=123456&client_secret=abcdef
响应示例:
HTTP/1.1 200 OK
Content-Type: application/json;charset=UTF-8
{
'access_token': 'xxxxxxxxxxxxxxx',
'token_type': 'Bearer',
'expires_in': 3600,
'refresh_token': 'yyyyyyyyyyyyyyy',
'scope': 'read write',
'openid': '123456'
}
原文地址: https://www.cveoy.top/t/topic/olXv 著作权归作者所有。请勿转载和采集!