tft每日頭條

 > 圖文

 > 怎麼用代碼登錄自己qq

怎麼用代碼登錄自己qq

圖文 更新时间:2024-08-13 21:20:50

怎麼用代碼登錄自己qq?1、首先開發者需要登錄QQ互聯,進行開發者認證,這裡需要,接下來我們就來聊聊關于怎麼用代碼登錄自己qq?以下内容大家不妨參考一二希望能幫到您!

怎麼用代碼登錄自己qq(使用最簡單的方式實現QQ登錄)1

怎麼用代碼登錄自己qq

前期準備

1、首先開發者需要登錄QQ互聯,進行開發者認證,這裡需要

個人基本信息

一張手持身份證的張片

2、進入應用管理頁面,依次點擊:應用管理 -> 網站應用 -> 創建應用,應用信息提交後,等待審核通過即可,這一步我們需要注意的是:

網站域名需要提前備案

網站信息要和備案信息一緻

QQ登錄實現

這裡為了簡單,我們使用JustAuth來實現QQ登錄,該項目集成了Github、Gitee、QQ、微博等等第三方登錄,号稱史上最全的整合第三方登錄的開源庫。

另外為了方便演示,就不使用SpringBoot了,隻用Vert.x搭建簡單的服務。

1、導入依賴,其中hutool是一個工具類庫

<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.3.3</version> </dependency> <dependency> <groupId>me.zhyd.oauth</groupId> <artifactId>JustAuth</artifactId> <version>1.15.2-alpha</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-web</artifactId> <version>3.2.0</version> </dependency>

2、實現服務端代碼

packagecom.qianyu; importcn.hutool.json.*; importio.vertx.core.*; importio.vertx.core.http.*; importio.vertx.ext.web.*; importme.zhyd.oauth.config.*; importme.zhyd.oauth.model.*; importme.zhyd.oauth.request.*; importme.zhyd.oauth.utils.*; publicclassServer{ privatestaticAuthQqRequestauthQqRequest; privatestaticAuthRequestcreateAuthRequest(){ if(authQqRequest==null){ authQqRequest=newAuthQqRequest(AuthConfig.builder() .clientId("你的clientid") .clientSecret("你的clientsecret") .redirectUri("你的回調地址") .build()); } returnauthQqRequest; } publicstaticvoidmain(String[]args){ Vertxvertx=Vertx.vertx(); Routerrouter=Router.router(vertx); router.get("/comm/user/callback").blockingHandler(context->{ httperverRequestreq=context.request(); AuthCallbackcallback=newAuthCallback(); callback.setCode(req.getParam("code");); callback.setState(req.getParam("state")); AuthRequestauthRequest=createAuthRequest(); AuthResponseauRes=authRequest.login(callback); httperverResponseres=context.response(); res.putHeader("Content-Type","text/json;charset=utf-8"); res.end(JSONUtil.toJsonStr(auRes)); }); router.get("/login").blockingHandler(context->{ Stringurl=createAuthRequest().authorize(AuthStateUtils.createState()); httperverResponseres=context.response(); res.putHeader("location",url); res.setStatusCode(302); res.end(); }); httperverhttperver=vertx.createhttperver(); httperver.requestHandler(router::accept); httperver.listen(8886); } }

代碼很好理解,主要可以分為以下幾個步驟

    構建一個QQ登錄的工具類,監聽兩個路由

    當我們訪問/login的時候,生成登錄地址,并且重定向到登錄地址

    當我們登錄之後,系統跳往回調地址,即/comm/user/callback,在這裡我們獲取參數code和state封裝成AuthCallback對象,執行登錄方法

    登錄成功後會返回用戶信息,格式如下:

登錄成功後返回的用戶信息

需要注意的是:創建AuthQqRequest對象的時候,必須是單例,也就是說,必須保證生成登錄地址的對象的執行登錄方法的對象是同一個。

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关圖文资讯推荐

热门圖文资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved