?歸類于:?Node.js
很多網(wǎng)站登錄頁面接入了第三方平臺(tái)的登錄,這對(duì)于我來說,非常友好,避免頻繁注冊、記錄網(wǎng)站的賬號(hào)。
在 koa2 項(xiàng)目中接入 gitee 的 OAuth 登錄筆記。
創(chuàng)建應(yīng)用
登錄https://gitee.com/個(gè)人中心。
第三方應(yīng)用-創(chuàng)建應(yīng)用。
應(yīng)用主頁填你的域名,如https://www.eoway.cn。
應(yīng)用回調(diào)地址是用來接收 gitee 授權(quán)成功后返回的code,如https://www.eoway.cn/oauth/giteeRedirectUri。

創(chuàng)建成功后,應(yīng)用詳情可以看到Client ID和Client Secret。
應(yīng)用詳情右下角有個(gè)模擬請求,點(diǎn)擊模擬請求會(huì)在新窗口打開頁面,這個(gè) url 就是此應(yīng)用授權(quán)登錄的地址。
可以在自己的網(wǎng)站登錄頁面加個(gè)按鈕,點(diǎn)擊按鈕跳轉(zhuǎn)到這個(gè)授權(quán)登錄 url。

流程
《OAuth 文檔》https://gitee.com/api/v5/oauth_doc#/
gitee 授權(quán)流程圖:

1、用戶打開授權(quán) url 并點(diǎn)擊同意授權(quán)。2、認(rèn)證服務(wù)器將頁面重定向到到回調(diào)地址,回調(diào)地址攜帶code。3、后端拿到code,使用code請求碼云認(rèn)證服務(wù)器獲取access_token。4、后端使用access_token請求 Open API 獲取用戶信息。
1、引導(dǎo)用戶授權(quán)
瀏覽器端,引導(dǎo)用戶打開以下授權(quán) url(即上面模擬請求的 url)。
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
client_id:授權(quán)碼。應(yīng)用的Client ID。redirect_uri:回調(diào)地址,應(yīng)用的登錄回調(diào)地址,后端定義這個(gè)地址用來接收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必須和應(yīng)用詳情的內(nèi)容一致,嘗試修改 redirect_uri,請求時(shí)報(bào)無效的登錄回調(diào)地址。
2、后端獲取 code
用戶打開了上面的授權(quán) url,點(diǎn)擊同意授權(quán)后,頁面重定向到應(yīng)用填寫的回調(diào)地址,并攜帶了授權(quán)碼code。
http://localhost:3000/oauth/giteeRedirectUri?code=07d02cdce70991b2db21d3b1742fb2e032456d2533111f05721e7a0ebc0c7a24
我用的是koa2,定義了一個(gè)路由/oauth/giteeRedirectUri來接收code。
沒有接收到code應(yīng)該是授權(quán)不成功、用戶取消授權(quán)等情況。
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 參數(shù):
參數(shù)說明
grant_type授權(quán)模式,這里用授權(quán)碼模式。授權(quán)碼模式:authorization_code。密碼模式:password。token模式:refresh_token。
code授權(quán)碼。
client_id應(yīng)用 client_id。
client_secret應(yīng)用 client_secret。
redirect_uri應(yīng)用回調(diào)地址,和應(yīng)用填寫的回調(diào)地址一致,不一致會(huì)導(dǎo)致獲取 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? }}
返回結(jié)果:
{
"data": {
"access_token": "5b9a1f119839ce46765ccf69r1a4884a",
"token_type": "bearer",
"expires_in": 86400,
"refresh_token": "3843c52e6d920da3444a9c342c49e55a7a2cde375dc2a85ebd9c08c5dee51f86",
"scope": "user_info",
"created_at": 1603357178
}
}

后端將access_token和refresh_token儲(chǔ)存。
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? }}
其他
文檔右上角有個(gè)申請授權(quán),可以在文檔內(nèi)測試請求接口。

轉(zhuǎn)載請注明來源:《 nodejs接入gitee碼云OAuth2登錄(第三方登錄)》- rojerYong's Blog
文章鏈接:https://www.eoway.cn /article/1603360705.html
如果此文摘取了你的原創(chuàng),請聯(lián)系本站管理員,將對(duì)此文修改、刪除處理。