GITEE 基于OAuth2的API V5版本

发布于:2024-04-29 ⋅ 阅读:(28) ⋅ 点赞:(0)
为了构建更好的码云生态环境,我们推出了基于OAuth2的API V5版本。
API V5接口使用方式以及Url都参照GitHub,为了各位开发者更好的兼容已经存在的第三方应用。

API 使用条款

  • OSCHINA 用户是资源的拥有者,需尊重和保护用户的权益。
  • 不能在应用中使用 OSCHINA 的名称。
  • 未经用户允许,不准爬取或存储用户的资源。
  • 禁止滥用 API,请求频率过快将导致请求终止。

OAuth2 认证基本流程

OAuth2 获取 AccessToken 认证步骤

1. 授权码模式
  • 应用通过 浏览器 或 Webview 将用户引导到码云三方认证页面上( GET请求https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
  • 用户对应用进行授权
    注意: 如果之前已经授权过的需要跳过授权页面,需要在上面第一步的 URL 加上 scope 参数,且 scope 的值需要和用户上次授权的勾选的一致。如用户在上次授权了user_info、projects以及pull_requests。则步骤A 中 GET 请求应为: https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=user_info%20projects%20pull_requests
  • 码云认证服务器通过回调地址{redirect_uri}将 用户授权码 传递给 应用服务器 或者直接在 Webview 中跳转到携带 用户授权码的回调地址上,Webview 直接获取code即可({redirect_uri}?code=abc&state=xyz)
  • 应用服务器 或 Webview 使用 access_token API 向 码云认证服务器发送post请求传入 用户授权码 以及 回调地址( POST请求 注:请求过程建议将 client_secret 放在 Body 中传值,以保证数据安全。 https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
  • 码云认证服务器返回 access_token
    应用通过 access_token 访问 Open API 使用用户数据。
  • 当 access_token 过期后(有效期为一天),你可以通过以下 refresh_token 方式重新获取 access_token( POST请求https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token}
  • 注意:如果获取 access_token 返回 403,可能是没有设置User-Agent的原因。
    详见:获取Token时服务端响应状态403是什么情况
2. 密码模式
  • 用户向客户端提供邮箱地址和密码。客户端将邮箱地址和密码发给码云认证服务器,并向码云认证服务器请求令牌。( POST请求。Content-Type: application/x-www-form-urlencodedcurl -X POST --data-urlencode "grant_type=password" --data-urlencode "username={email}" --data-urlencode "password={password}" --data-urlencode "client_id={client_id}" --data-urlencode "client_secret={client_secret}" --data-urlencode "scope=projects user_info issues notes" https://gitee.com/oauth/token
    scope表示权限范围,有以下选项,请求时使用空格隔开 user_info projects pull_requests issues notes keys hook groups gists enterprises
  • 码云认证服务器返回 access_token
    应用通过 access_token 访问 Open API 使用用户数据。

创建应用流程

  • 修改资料 -> 第三方应用,创建要接入码云的应用。

  • 填写应用相关信息,勾选应用所需要的权限。其中: 回调地址是用户授权后,码云回调到应用,并且回传授权码的地址。

  • 创建成功后,会生成 Cliend IDClient Secret。他们将会在上述OAuth2 认证基本流程用到。


网站公告

今日签到

点亮在社区的每一天
去签到