归类于: Node.js
很多网站登录页面接入了第三方平台的登录,这对于我来说,非常友好,避免频繁注册、记录网站的账号。
在 koa2 项目中接入 gitee 的 OAuth 登录笔记。
创建应用
登录https://gitee.com/个人中心。
第三方应用-创建应用。
应用主页填你的域名,如https://www.eoway.cn。
应用回调地址是用来接收 gitee 授权成功后返回的code,如https://www.eoway.cn/oauth/giteeRedirectUri。
创建成功后,应用详情可以看到Client ID和Client Secret。
应用详情右下角有个模拟请求,点击模拟请求会在新窗口打开页面,这个 url 就是此应用授权登录的地址。
可以在自己的网站登录页面加个按钮,点击按钮跳转到这个授权登录 url。
流程
《OAuth 文档》https://gitee.com/api/v5/oauth_doc#/
gitee 授权流程图:
1、用户打开授权 url 并点击同意授权。2、认证服务器将页面重定向到到回调地址,回调地址携带code。3、后端拿到code,使用code请求码云认证服务器获取access_token。4、后端使用access_token请求 Open API 获取用户信息。
1、引导用户授权
浏览器端,引导用户打开以下授权 url(即上面模拟请求的 url)。
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
client_id:授权码。应用的Client ID。redirect_uri:回调地址,应用的登录回调地址,后端定义这个地址用来接收code。如:https://gitee.com/oauth/authorize?client_id=6a5de72e34864a1c11b107e1e33d0465e97f7530ad9b5b79eb330cdc45e4ba44&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Foauth%2FgiteeRedirectUri&response_type=code
client_id 和 redirect_uri必须和应用详情的内容一致,尝试修改 redirect_uri,请求时报无效的登录回调地址。
2、后端获取 code
用户打开了上面的授权 url,点击同意授权后,页面重定向到应用填写的回调地址,并携带了授权码code。
http://localhost:3000/oauth/giteeRedirectUri?code=07d02cdce70991b2db21d3b1742fb2e032456d2533111f05721e7a0ebc0c7a24
我用的是koa2,定义了一个路由/oauth/giteeRedirectUri来接收code。
没有接收到code应该是授权不成功、用户取消授权等情况。
module.exports =async(ctx, next) => {letcode = ctx.query.code ||null// ...}
3、使用 code 获取 access_token
post 请求下面的地址。
https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&client_secret={client_secret}&redirect_uri={redirect_uri}
url 请求地址:
data 参数:
参数说明
grant_type授权模式,这里用授权码模式。授权码模式:authorization_code。密码模式:password。token模式:refresh_token。
code授权码。
client_id应用 client_id。
client_secret应用 client_secret。
redirect_uri应用回调地址,和应用填写的回调地址一致,不一致会导致获取 access_token 失败。
constaxios =require('axios');module.exports =async(ctx, next) => {letdata =nullletcode = ctx.query.code ||nullletclient_id ='d54c61c6f3665d53572d945d0548e982d59030c4f49b0d033e155bcc8df29122'letclient_secret ='cdf380f874b13e6fa12416da7db088cf4246b68242766eea29ad49e444e2b076'letredirect_uri ='http://localhost:3000/oauth/giteeRedirectUri'if(code) {letoption = {method:'post',url:`https://gitee.com/oauth/token`,data: {grant_type:'authorization_code', code, client_id, client_secret, redirect_uri } }awaitaxios(option).then(res =>{if(res.status ==200) { data = res.data } }) } ctx.body = { data }}
返回结果:
{
"data": {
"access_token": "5b9a1f119839ce46765ccf69r1a4884a",
"token_type": "bearer",
"expires_in": 86400,
"refresh_token": "3843c52e6d920da3444a9c342c49e55a7a2cde375dc2a85ebd9c08c5dee51f86",
"scope": "user_info",
"created_at": 1603357178
}
}
后端将access_token和refresh_token储存。
4、使用 access_token 获取用户信息
《API 文档》https://gitee.com/api/v5/swagger#/getV5User
get 请求下面地址。
https://gitee.com/api/v5/user?access_token={access_token}
constaxios =require('axios');module.exports =async(ctx, next) => {letdata =nullletoption = {method:'get',url:`https://gitee.com/api/v5/user`,params:{access_token:'e0cdb1c73653b56b672db066ab56c303'} }awaitaxios(option).then(res =>{if(res.status ==200) { data = res.data } }) ctx.body = { data }}
重新获取 access_token
access_token 有效期为一天,在 access_token 过期后,不需要用户登录的情况下,可以使用 refresh_token 重新获取 access_token。
post请求下面的地址,重新获取 access_token。
https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token}
constaxios =require('axios');module.exports =async(ctx, next) => {letdata =nullletoption = {method:'post',url:`https://gitee.com/oauth/token`,data: {grant_type:'refresh_token',refresh_token:'c54a03269665a5e105ea29271c23af28bc45a537d97f535a1fe13131ed40d4bd'} }awaitaxios(option).then(res =>{if(res.status ==200) { data = res.data } }) ctx.body = { data }}
其他
文档右上角有个申请授权,可以在文档内测试请求接口。
转载请注明来源:《 nodejs接入gitee码云OAuth2登录(第三方登录)》- rojerYong's Blog
文章链接:https://www.eoway.cn /article/1603360705.html
如果此文摘取了你的原创,请联系本站管理员,将对此文修改、删除处理。