欧美亚洲中文,在线国自产视频,欧洲一区在线观看视频,亚洲综合中文字幕在线观看

      1. <dfn id="rfwes"></dfn>
          <object id="rfwes"></object>
        1. 站長資訊網(wǎng)
          最全最豐富的資訊網(wǎng)站

          淺析node怎么進行微博第三方登錄

          怎么進行微博第三方登錄?下面本篇文章給大家介紹一下使用node實現(xiàn)微博第三方登錄的方法,希望對大家有所幫助!

          淺析node怎么進行微博第三方登錄

          node.js極速入門課程:進入學習

          接入微博第三方登錄可以免注冊,對用戶的體驗更好,今天我們就用nodejs實現(xiàn)微博第三方登錄(用其它語言也可以)?!鞠嚓P教程推薦:nodejs視頻教程】

          實現(xiàn)效果

          在線實例: http://www.lolmbbs.com/login

          1、點擊微博登錄按鈕登錄

          淺析node怎么進行微博第三方登錄

          2、直接掃碼登錄

          具體實現(xiàn)

          一、申請weibo網(wǎng)站接入

          登錄https://open.weibo.com/connect申請web網(wǎng)站接入
          本地開發(fā)的時候應用地址寫:127.0.0.1

          淺析node怎么進行微博第三方登錄
          淺析node怎么進行微博第三方登錄
          淺析node怎么進行微博第三方登錄

          二、點擊按鈕微博登錄

          采用OAuth2.0授權,詳細可參考文檔https://open.weibo.com/wiki/Connect/login

          1. 生成微博登錄授權驗證碼

          const weiboUrl = `https://api.weibo.com/oauth2/authorize?client_id=${weiboConfig.appKey}&response_type=code&redirect_uri=${weiboConfig.redirectUrl}`
          登錄后復制

          appKey: 創(chuàng)建應用成功后weibo給你的appKey
          redirectUrl: 用戶授權成功后跳轉的你的前端頁面,我這里寫的是http://127.0.0.1:8080/login

          2. 授權頁面跳轉,獲取用戶code

          用戶授權登錄后,會跳轉到你上一步寫的redirectUrl,并帶上用戶code,url類似于http://127.0.0.1:8080/login?code=abcdef

          vue監(jiān)聽路由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");       });     }   }
          登錄后復制

          3. 后端登錄回調接口,通過用戶code獲取accessToken,再通過accessToken獲取用戶信息,完成登錄

             async loginCallback(ctx) {       let { code } = ctx.request.body       if (!code) {          return ctx.error(errCode.PARAMS_ERROR, '參數(shù)錯誤')       }       // 獲取accessToken       const { access_token, uid } = await got.post('https://api.weibo.com/oauth2/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 } = await got.get(`https://api.weibo.com/2/users/show.json?access_token=${access_token}&uid=${uid}`).json()       // 在自己的系統(tǒng)內創(chuàng)建User       let [user, isCreate] = await WeiboUser.upsert({ id, nickname, avatar })       // 生成登錄Token,通過userType區(qū)分是微博登錄用戶還是系統(tǒng)賬號登錄用戶       const token = await jwt.createToken({ ...user.toJSON(), userType: 'weiboUser' })       return ctx.success({ nickname, avatar, token })    }
          登錄后復制

          三、微博掃碼登錄

          1. 生成微博掃碼登錄二維碼

           async getWeiboLoginQr(ctx) {       const qrApi = `https://api.weibo.com/oauth2/qrcode_authorize/generate?client_id=${weiboConfig.appKey}&redirect_uri=${weiboConfig.redirectUrl}&scope=&response_type=code&state=&__rnd=${Date.now()}`       const { url, vcode } = await got.get(qrApi).json()       return ctx.success({ weiboQrUrl: url, vcode }) }
          登錄后復制

          返回的url就是微博登錄二維碼url,vcode相當于此二維碼唯一標識,用來查詢用戶是否掃碼

          2. 前端不停輪詢,查詢此二維碼是否被掃碼授權

          前端:

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

          后端:

             async getWeiboLoginQrStatus(ctx) {       const { vcode } = ctx.request.query       if (!vcode) {          return ctx.error(errCode.PARAMS_ERROR, '參數(shù)錯誤')       }       const queryQrApi = `https://api.weibo.com/oauth2/qrcode_authorize/query?vcode=${vcode}&__rnd=${Date.now()}`       const { status, url } = await got(queryQrApi).json()       return ctx.success({ status, url })    }
          登錄后復制

          如果status為3,代碼用戶已經(jīng)掃碼授權了,同時返回的url即點擊按鈕登錄后的前端回調url。之后的步驟就跟2. 授權頁面跳轉,獲取用戶code一模一樣了.

          贊(0)
          分享到: 更多 (0)
          網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號