第三方登录的原理
所谓第三方登录,实质就是 OAuth 授权。用户想要登录 A 网站,A 网站让用户提供第三方网站的数据,证明自己的身份。获取第三方网站的身份数据,就需要 OAuth 授权。
举例来说,A 网站允许 Gitee 登录,背后就是下面的流程。
- A 网站让用户跳转到 Gitee。
- Gitee 要求用户登录,然后询问"A 网站要求获得 xx 权限,你是否同意?"
- 用户同意,Gitee 就会重定向回 A 网站,同时发回一个授权码。
- A 网站使用授权码,向 Gitee 请求令牌。
- Gitee 返回令牌.
- A 网站使用令牌,向 Gitee 请求用户数据。
gitee中创建应用
- 设置中点击【第三方应用】
- 点击【创建应用】
- 填写信息,然后点击【创建应用】
浏览器跳转gitee
写了一个html页面,只有一个链接按钮:
链接的跳转地址为:
https://gitee.com/oauth/authoriclient_id=d898004fd16f0f0552578023587e98106d3f5b738e956fea31792406bf7a457d&redirect_uri=http://localhost:5000/giteeredirect.html&response_type=code
点击链接后,跳转到【OAuth 授权请求】页面:
点击【同意授权】后,跳转到redirect_uri指定的地址,并且带有授权码:
http://localhost:5000/giteeredirect.html?code=7396b4fbfeb3ba669827a75421b528a90a2b6a5542f887ad8ea33187dd6bf819
根据授权码获取accesstoken
获取token的地址为:
https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
这里直接用postman请求:
根据token获取用户信息
接口地址为:https://gitee.com/api/v5/user
使用postman请求如下:
gitee文档地址:https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoStargazers?ex=no