web网站接入Gitee第三方登录
官方文档地址
API文档:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoStargazers?ex=no
OAuth文档:https://gitee.com/api/v5/oauth_doc
创建应用
如何创建应用就不说了,与github类似,
OAuth2 获取 AccessToken 认证步骤
- 应用通过 浏览器 或 Webview 将用户引导到码云三方认证页面上( GET请求 )
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
- 用户对应用进行授权
- 码云认证服务器通过回调地址{redirect_uri}将 用户授权码 传递给 应用服务器 或者直接在 Webview 中跳转到携带 用户授权码的回调地址上,Webview 直接获取code即可
({redirect_uri}?code=abc&state=xyz)
- 应用服务器 或 Webview 使用 access_token API 向 码云认证服务器发送post请求传入 用户授权码 以及 回调地址( POST请求 )
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获取用户信息
GET请求 模拟http get请求https://gitee.com/api/v5/user?access_token={access_token}
奉上源码
// 用户登录
public function gitee()
{
if (isset($_GET['code'])) {
$access_token_url = 'https://gitee.com/oauth/token';
$params = array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'client_id' => self::GITEE_CLIENT_ID,
'redirect_uri' => 'https://www.whongbin.cn/admin/oauth/gitee.html',
'client_secret' => self::GITEE_CLIENT_SECRET
);
$res = json_decode(getHttpResponsePOST($access_token_url, $params),true);
$access_token = $res['access_token'];
if ($access_token) {
$info_url = 'https://gitee.com/api/v5/user?access_token='.$access_token;
$info = json_decode(getHttpResponseGET($info_url),true);
// 处理数据
}
}
}
上面代码中的curl请求在这篇文章中已经贴出来了 web网站接入GitHub第三方登录(踩坑)
评论 (0)