第一步:先去qq互联进行创建网站应用:QQ互联 如下图:
第二步:引入qq的jar包,这里采用maven方式引用
<!--QQ坐标--><dependency><groupId>net.gplatform</groupId><artifactId>Sdk4J</artifactId><version>2.0</version></dependency>
第三步:将资源文件放到如下图目录下面,注意名字(qqconnectconfig.properties)不能改变
qqconnectconfig.properties内容只能改前三行信息:
## qq用户授权信息
app_ID = 移动应用的appid
app_KEY = 移动应用的appkey
redirect_URI = 授权成功后的回调地址
scope = get_user_info,add_topic,add_one_blog,add_album,upload_pic,list_album,add_share,check_page_fans,add_t,add_pic_t,del_t,get_info,get_other_info,get_fanslist,get_idollist,add_idol,del_idol,get_tenpay_addr,get_repost_list
baseURL = https://graph.qq.com/
getUserInfoURL = https://graph.qq.com/user/get_user_info
accessTokenURL = https://graph.qq.com/oauth2.0/token
authorizeURL = https://graph.qq.com/oauth2.0/authorize
getOpenIDURL= https://graph.qq.com/oauth2.0/me
addTopicURL = https://graph.qq.com/shuoshuo/add_topic
addBlogURL = https://graph.qq.com/blog/add_one_blog
addAlbumURL = https://graph.qq.com/photo/add_album
uploadPicURL = https://graph.qq.com/photo/upload_pic
listAlbumURL = https://graph.qq.com/photo/list_album
addShareURL = https://graph.qq.com/share/add_share
checkPageFansURL = https://graph.qq.com/user/check_page_fans
awdTURL = https://graph.qq.com/t/add_t
addPicTURL = https://graph.qq.com/t/add_pic_t
delTURL = https://graph.qq.com/t/del_t
getWeibouserInfouRL = https://graph.qq.com/user/get_info
getweibootherUserInfouRl = https://graph.qq.com/user/get_other_info
getFansListURL = https://graph.qq.com/relation/get_fanslist
getIdolsListURL = https://graph.qq.com/relation/get_idollist
addIdoluRL = https://graph.qq.com/relation/add_idol
delIdolURL = https://graph.qq.com/relation/del_idol
getTenpayAddrURL = https://graph.qq.com/cft_info/get_tenpay_addr
getRepostListURL = https://graph.qq.com/t/get_repost_list
version = 2.0.0.0
第四步:上controller代码
/*** @description: 获取qq授权链接* @return:*/@Login(false)@ApiOperation("获取qq授权链接")@GetMapping("/getQQAuthorizationUrl")public void getQQAuthorizationUrl(HttpServletRequest request, HttpServletResponse response) {response.setContentType("text/html;charset=utf-8");try {response.sendRedirect(new Oauth().getAuthorizeURL(request));} catch (QQConnectException | IOException e) {e.printStackTrace();logger.error("获取QQ授权链接失败!", e.getMessage());}}
第五步:获取授权的qq用户信息
/*** @description: 拉取qq授权用户信息* @return:*/public ResultVO<QqInfoDto> qqAuthorizateCabllBack(HttpServletRequest request, HttpServletResponse response) throws QQConnectException {QqInfoDto qqInfoDto = new QqInfoDto();AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);String accessToken = null, openID = null;// 用户授权的时候取消了if (StringUtils.isEmpty(accessTokenObj.getAccessToken())) {return ResultVO.error("用户取消授权!");}accessToken = accessTokenObj.getAccessToken();// 用户QQ的个人信息OpenID openIDObj = new OpenID(accessToken);openID = openIDObj.getUserOpenID(); // onpenId是QQ用户的唯一标示UserInfo qzoneUserInfo = new UserInfo(accessToken, openID); //TOKENUserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();if(userInfoBean.getRet() < 0){return ResultVO.error(userInfoBean.getMsg());}BeanUtil.copyProperties(userInfoBean, qqInfoDto);return ResultVO.success("获取成功!", qqInfoDto);}
QqInfoDto返回实体类
package com.sport.sportcloudmarathonh5.dto;import com.qq.connect.javabeans.Avatar;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import java.io.Serializable;/*** @description: 微信授权用户信息* @author: zdj* @date: 2022/11/29 13:55* @Version:V1.0**/
@Data
public class QqInfoDto implements Serializable {@ApiModelProperty(value = "头像集合",hidden = true)private Avatar avatar = new Avatar("");@ApiModelProperty(value = "昵称", hidden = true)private String nickname;@ApiModelProperty(value = "性别(默认‘男’)",hidden = true)private String gender;@ApiModelProperty(value = "vip",hidden = true)private boolean vip;@ApiModelProperty(value = "level",hidden = true)private int level;@ApiModelProperty(value = "yellowYearVip",hidden = true)private boolean yellowYearVip;
}
全局返回实体ResultVO
package com.sport.sportcloudmarathonh5.vo;import java.io.Serializable;/*** @author zdj* @version 1.0* @date 2021-10-31 19:42:33*/
public class ResultVO<T> implements Serializable {private Integer code;private String message;private T data;private static <T> ResultVO<T> getInstance() {return new ResultVO<T>();}public static <T> ResultVO<T> token() {return response(300, "请登录", null);}public static <T> ResultVO<T> success() {return response(200, "success", null);}public static <T> ResultVO<T> success(T data) {return response(200, "success", data);}public static <T> ResultVO<T> success(String message, T data) {return response(200, message, data);}public static <T> ResultVO<T> error() {return response(500, "服务器异常", null);}public static <T> ResultVO<T> error(String message) {return response(500, message, null);}public static <T> ResultVO<T> error(String message, T data) {return response(500, message, data);}public static <T> ResultVO<T> exp() {return response(500, "服务器异常", null);}public static <T> ResultVO<T> response(Integer code, String message, T data) {ResultVO<T> instance = getInstance();instance.setCode(code);instance.setMessage(message);instance.setData(data);return instance;}public Integer getCode() {return code;}public ResultVO<T> setCode(Integer code) {this.code = code;return this;}public String getMessage() {return message;}public ResultVO<T> setMessage(String message) {this.message = message;return this;}public T getData() {return data;}public ResultVO<T> setData(T data) {this.data = data;return this;}
}
注释:qq授权登录java版本目前官方不提供了,所以这里记录下,方便后期使用