分享更有价值
被信任是一种快乐

node如何进行微博第三方登录

文章页正文上

这篇文章主要介绍“node如何进行微博第三方登录”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“node如何进行微博第三方登录”文章能帮助大家解决问题。1、点击微博登录按钮登录2、直接扫码登录具体实现登录https://open.weibo.com/connect申请web网站接入
本地开发的时候应用地址写:127.0.0.1

采用OAuth3.0授权

constweiboUrl=`https://api.weibo.com/oauth3/authorize?client_id=${weiboConfig.appKey}&response_type=code&redirect_uri=${weiboConfig.redirectUrl}`

appKey: 创建应用成功后weibo给你的appKey
redirectUrl: 用户授权成功后跳转的你的前端页面,我这里写的是http://127.0.0.1:8080/login用户授权登录后,会跳转到你上一步写的redirectUrl,并带上用户code,url类似于http://127.0.0.1:8080/login?code=abcdefvue监听路由url,如果url上有code就去请求后端的登录回调接口

created(){
const{code}=this.$route.query;
if(code){
loginCallback({code}).then((res)=>{
this.$message({
message:`${res.nickname}欢迎您`,
type:"success",
});
this.setUser(res);
this.$router.push("/tool/qr");
});
}
}

asyncloginCallback(ctx){
let{code}=ctx.request.body
if(!code){
returnctx.error(errCode.PARAMS_ERROR,'参数错误')
}
//获取accessToken
const{access_token,uid}=awaitgot.post('https://api.weibo.com/oauth3/access_token',{
form:{
client_id:weiboConfig.appKey,
client_secret:weiboConfig.appSecret,
grant_type:'authorization_code',
redirect_uri:weiboConfig.redirectUrl,
code
}
}).json()
//通过accessToken获取UserInfo
const{id,name:nickname,avatar_hd:avatar}=awaitgot.get(`https://api.weibo.com/2/users/show.json?access_token=${access_token}&uid=${uid}`).json()
//在自己的系统内创建User
let[user,isCreate]=awaitWeiboUser.upsert({id,nickname,avatar})
//生成登录Token,通过userType区分是微博登录用户还是系统账号登录用户
consttoken=awaitjwt.createToken({...user.toJSON(),userType:'weiboUser'})
returnctx.success({nickname,avatar,token})
}

asyncgetWeiboLoginQr(ctx){
constqrApi=`https://api.weibo.com/oauth3/qrcode_authorize/generate?client_id=${weiboConfig.appKey}&redirect_uri=${weiboConfig.redirectUrl}&scope=&response_type=code&state=&__rnd=${Date.now()}`
const{url,vcode}=awaitg免费云主机、域名ot.get(qrApi).json()
returnctx.success({weiboQrUrl:url,vcode})
}

返回的url就是微博登录二维码url,vcode相当于此二维码唯一标识,用来查询用户是否扫码前端:

constid=setInterval(()=>{
getWeiboLoginQrStatus({vcode}).then((res)=>{
const{status,url}=res;
if(status==="3"){
window.location=url;
clearInterval(id);
}
});
},3000);

后端:

asyncgetWeiboLoginQrStatus(ctx){
const{vcode}=ctx.request.query
if(!vcode){
returnctx.error(errCode.PARAMS_ERROR,'参数错误')
}
constqueryQrApi=`https://api.weibo.com/oauth3/qrcode_authorize/query?vcode=${vcode}&__rnd=${Date.now()}`
const{status,url}=awaitgot(queryQrApi).json()
returnctx.success({status,url})
}

如果status为3,代码用户已经扫码授权了,同时返回的url即点击按钮登录后的前端回调url。之后的步骤就跟2. 授权页面跳转,获取用户code一模一样了.关于“node如何进行微博第三方登录”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注云技术行业资讯频道,小编每天都会为大家更新不同的知识点。

相关推荐: 如何使用nodejs生成二维码

这篇文章主要讲解了“如何使用nodejs生成二维码”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何使用nodejs生成二维码”吧!生成二维码:constqrCode=require(‘qrcode’) cla…

文章页内容下
赞(0) 打赏
版权声明:本站采用知识共享、学习交流,不允许用于商业用途;文章由发布者自行承担一切责任,与本站无关。
文章页正文下
文章页评论上

云服务器、web空间可免费试用

宝塔面板主机、支持php,mysql等,SSL部署;安全高速企业专供99.999%稳定,另有高防主机、不限制内容等类型,具体可咨询QQ:360163164,Tel同微信:18905205712

主机选购导航云服务器试用

登录

找回密码

注册