JavaQQ授权登录Demo ,整合官网直接运行版本
概述
详细
详细
功能演示
一、相关配置
QQ APP ID(QQ互联官网-->应用管理-->APP ID)
QQ APP KEY (QQ互联官网-->应用管理-->APP KEY)
QQ 网站回调域 (QQ互联官网-->应用管理-->网站回调)
application.properties 文件 (Demo上都有默认值,不需要修改直接使用,省心!)
网站开发流程
http://wiki.connect.qq.com/%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C_oauth2-0
SDK与DEMO下载
http://wiki.connect.qq.com/sdk%E4%B8%8B%E8%BD%BD
二、目录结构
三、准备工作
修改本地电脑host文件
下面以 win10系统 为例子,打开路径:c:\windows\system32\drivers\etc
选择 hosts 文件, 右键-->打开方式-->记事本
修改前 :
在最下面空白处添加:127.0.0.1 www.lvjingwei.xyz
修改后 :
四、功能测试
访问首页 : www.lvjingwei.xyz
点击 --> QQ 网页授权
点击 --> 头像
五、功能讲解
流程图:
(1)获取Access_Token
//获取Access_Token String tokenURL = "https://graph.qq.com/oauth2.0/token"; tokenURL += "?grant_type=authorization_code"; tokenURL += "&client_id="+appID; tokenURL += "&client_secret="+appKey; tokenURL += "&code="+code; tokenURL += "&redirect_uri="+redirectURI; String accessTokenString = HttpsClient.httpsRequestReturnString(tokenURL,HttpsClient.METHOD_GET,null);
更多的信息,请看官网
官网相关链接: http://wiki.connect.qq.com/%E4%BD%BF%E7%94%A8authorization_code%E8%8E%B7%E5%8F%96access_token
(2)获取用户OpenID
//获取用户OpenID String openURL = "https://graph.qq.com/oauth2.0/me"; openURL += "?access_token="+ accessToken; String openIDString = HttpsClient.httpsRequestReturnString(openURL,HttpsClient.METHOD_GET,null); String openID = CommonUtil.getOpenID(openIDString);
更多的信息,请看官网
官网相关链接:
http://wiki.connect.qq.com/%E8%8E%B7%E5%8F%96%E7%94%A8%E6%88%B7openid_oauth2-0
(3)获取登录用户的昵称、头像、性别
//获取登录用户的昵称、头像、性别 String getUserInfoURL = "https://graph.qq.com/user/get_user_info"; getUserInfoURL += "?access_token="+accessToken; getUserInfoURL += "&oauth_consumer_key="+appID; getUserInfoURL += "&openid="+openID; getUserInfoURL += "&format=json"; JSONObject infoJSONObject = HttpsClient.httpsRequestReturnJSONObject(getUserInfoURL,HttpsClient.METHOD_GET,null); if(infoJSONObject != null && infoJSONObject.get("ret") != null && StringUtils.equals(infoJSONObject.getString("ret"),"0")){ model.addAttribute("nickname",infoJSONObject.getString("nickname")); model.addAttribute("figureurl",infoJSONObject.getString("figureurl")); model.addAttribute("figureurl_1",infoJSONObject.getString("figureurl_1")); model.addAttribute("figureurl_2",infoJSONObject.getString("figureurl_2")); model.addAttribute("figureurl_qq_1",infoJSONObject.getString("figureurl_qq_1")); model.addAttribute("figureurl_qq_2",infoJSONObject.getString("figureurl_qq_2")); model.addAttribute("gender",infoJSONObject.getString("gender")); model.addAttribute("is_yellow_vip",infoJSONObject.getString("is_yellow_vip")); model.addAttribute("vip",infoJSONObject.getString("vip")); model.addAttribute("yellow_vip_level",infoJSONObject.getString("yellow_vip_level")); model.addAttribute("level",infoJSONObject.getString("level")); model.addAttribute("is_yellow_year_vip",infoJSONObject.getString("is_yellow_year_vip")); model.addAttribute("city",infoJSONObject.getString("city")); model.addAttribute("province",infoJSONObject.getString("province")); model.addAttribute("year",infoJSONObject.getString("year")); model.addAttribute("is_lost",infoJSONObject.getString("is_lost")); }else{ System.out.println("获取 获取登录用户的昵称、头像、性别 失败, 请检查参数!"); }
更多的信息,请看官网
官网相关链接:
http://wiki.connect.qq.com/get_user_info
(4)前端页面
index.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head > <title>QQ网页授权</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script type='text/javascript'> </script> </head> <body> <h1><a th:href="${qqAuthURL}">QQ网页授权</a></h1> </body> </html>
info.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head > <title>获取登录用户的昵称、头像、性别</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p><strong th:text="${nickname}"></strong> 用户在QQ空间的昵称。</p> <p><strong th:text="${gender}"></strong> 性别。 如果获取不到则默认返回"男"</p> <p><strong th:text="${province}"></strong> 省份</p> <p><strong th:text="${city}"></strong> 城市</p> <p><strong th:text="${year}"></strong> 年份</p> <p><strong th:text="${figureurl}"></strong> 大小为30×30像素的QQ空间头像URL。</p> <p><strong th:text="${figureurl_1}"></strong> 大小为50×50像素的QQ空间头像URL。</p> <p><strong th:text="${figureurl_2}"></strong> 大小为100×100像素的QQ空间头像URL。</p> <p><strong th:text="${figureurl_qq_1}"></strong> 大小为40×40像素的QQ头像URL。</p> <p><strong th:text="${figureurl_qq_2}"></strong> 大小为100×100像素的QQ头像URL。需要注意,不是所有的用户都拥有QQ的100x100的头像,但40x40像素则是一定会有。</p> <p><strong th:text="${is_yellow_vip}"></strong> 标识用户是否为黄钻用户(0:不是;1:是)</p> <p><strong th:text="${vip}"></strong> 标识用户是否为黄钻用户(0:不是;1:是)</p> <p><strong th:text="${yellow_vip_level}"></strong> 黄钻等级</p> <p><strong th:text="${level}"></strong> 黄钻等级</p> <p><strong th:text="${is_yellow_year_vip}"></strong> 标识是否为年费黄钻用户(0:不是; 1:是)</p> <p><strong th:text="${is_lost}"></strong> 是否丢失</p> </body> </html>
吐槽一下:QQ的接口API文档,写得太垃圾了,各种不清晰。SDK 也是太过复杂了,没有必要,上面的例子是参考过 官网SDK 再优化的,简单易懂。
谢谢大家观看~