diff --git a/_sidebar.md b/_sidebar.md index 4bd4696..2727ae1 100644 --- a/_sidebar.md +++ b/_sidebar.md @@ -1,4 +1,4 @@ - [开源项目使用手册](./git.md) - [创客使用手册](./chuangke.md) - [开源合规科普](./compliance.md) - +- [OAuth2 API](./oauth2.md) diff --git a/oauth2.md b/oauth2.md index e0946e5..00b7ac5 100644 --- a/oauth2.md +++ b/oauth2.md @@ -12,26 +12,36 @@ OAuth2 认证基本流程 ### 1. 授权码模式 * 应用通过 浏览器 或 Webview 将用户引导到红山三方认证页面上( GET请求 ) -https://www.osredm.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code + + `https://www.osredm.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code` + * 用户对应用进行授权 注意: 如果之前已经授权过的需要跳过授权页面,需要在上面第一步的 URL 加上 scope 参数,且 scope 的值需要和用户上次授权的勾选的一致。如用户在上次授权了user_info。则步骤A 中 GET 请求应为: -https://www.osredm.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=user_info -* 红山认证服务器通过回调地址{redirect_uri}将 用户授权码 传递给 应用服务器 或者直接在 Webview 中跳转到携带 用户授权码的回调地址上,Webview 直接获取code即可({redirect_uri}?code=abc&state=xyz) + + `https://www.osredm.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code&scope=user_info` + +* 红山认证服务器通过回调地址`{redirect_uri}`将 用户授权码 传递给 应用服务器 或者直接在 Webview 中跳转到携带 用户授权码的回调地址上,Webview 直接获取code即可(`{redirect_uri}?code=abc&state=xyz`) * 应用服务器 或 Webview 使用 access_token API 向 红山认证服务器发送post请求传入 用户授权码 以及 回调地址( POST请求 ) 注:请求过程建议将 client_secret 放在 Body 中传值,以保证数据安全。 -https://www.osredm.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://www.osredm.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token} -注意:如果获取 access_token 返回 403,可能是没有设置User-Agent的原因。 + + `https://www.osredm.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://www.osredm.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-urlencoded ) -curl -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=user_info" https://www.osredm.com/oauth/token -scope表示权限范围,有以下选项,请求时使用空格隔开 -user_info projects pull_requests issues notes keys hook groups gists enterprises + `curl -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=user_info" https://www.osredm.com/oauth/token` + + scope表示权限范围,有以下选项,请求时使用空格隔开 + `user_info` `projects` `pull_requests` `issues` `notes` `keys` `hook groups` `gists` `enterprises` * 认证服务器返回 access_token 应用通过 access_token 访问 Open API 使用用户数据。 \ No newline at end of file