admin管理员组文章数量:1640010
1.首先先注册账号,然后填资料审核,等个三四天吧大概
https://connect.qq/
2.审核通过后点击创建应用,把备案号什么的都填(这个审核两天之内一般)
!!! 这里有个重点,网站地址只能填一下,回调地址随便填一个,通过后可以随便改的
3. 整理java代码
我的是springBoot项目 先导入依赖,官网好像没有java版本的jar包了,我是找个网上坐标
<!--QQ登录的包sdk-->
<dependency>
<groupId>net.gplatform</groupId>
<artifactId>Sdk4J</artifactId>
<version>2.0</version>
</dependency>
然后编写创建一个配置文件 qqconnectconfig.properties 名字建议一致,改完了以后复制粘贴即可
app_ID = 填成自己的ID
app_KEY = 填成自己的KEY
redirect_URI = 填成自己的回调地址
grant_type=authorization_code
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_repost_list,get_info,get_other_info,get_fanslist,get_idollist,add_idol,del_ido,get_tenpay_addr
baseURL = https://graph.qq/
getUserInfoURL = https://graph.qq/user/get_user_info
accessTokenURL = https://graph.qq/oauth2.0/token
authorizeURL = https://graph.qq/oauth2.0/authorize
getOpenIDURL = https://graph.qq/oauth2.0/me
addTopicURL = https://graph.qq/shuoshuo/add_topic
addBlogURL = https://graph.qq/blog/add_one_blog
addAlbumURL = https://graph.qq/photo/add_album
uploadPicURL = https://graph.qq/photo/upload_pic
listAlbumURL = https://graph.qq/photo/list_album
addShareURL = https://graph.qq/share/add_share
checkPageFansURL = https://graph.qq/user/check_page_fans
addTURL = https://graph.qq/t/add_t
addPicTURL = https://graph.qq/t/add_pic_t
delTURL = https://graph.qq/t/del_t
getWeiboUserInfoURL = https://graph.qq/user/get_info
getWeiboOtherUserInfoURL = https://graph.qq/user/get_other_info
getFansListURL = https://graph.qq/relation/get_fanslist
getIdolsListURL = https://graph.qq/relation/get_idollist
addIdolURL = https://graph.qq/relation/add_idol
delIdolURL = https://graph.qq/relation/del_idol
getTenpayAddrURL = https://graph.qq/cft_info/get_tenpay_addr
getRepostListURL = https://graph.qq/t/get_repost_list
version = 2.0.0.0
前端代码 这里的话 根据自己的实际情况
<a href="/qqLogin" onclick="layer.msg('正在通过QQ登入', {icon:16, shade: 0.1, time:0})" >
<img class="avatar size-S" src="img/qq.jpg" title="登陆">登陆
</a>
后端java 跳转代码 这个直接复制粘贴记性
//QQ登录的跳转
@GetMapping("/qqLogin")
public String qqLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("text/html;charset=utf-8");
try {
String authorizeURL = new Oauth().getAuthorizeURL(request);
return "redirect:" + authorizeURL;
} catch (Exception e) {
return null;
}
这个时候网页是可以用的
然后编写java的回调代码
Crotroller类
//QQ登录的回调
@GetMapping("/qqLoginBack")
public String qqLoginBack(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
String code = request.getParameter("code");
//获取到了用户信息
Member member = QqService.LoginBack(code);
return "redirect:/";
}
services类,可能是jar版本太老了,jar的方法都不生效了,我就自己看着官方api文档发请求写了一个
@PropertySource("classpath:qqconnectconfig.properties")
@Service
public class QQServiceImpl {
@Value("${app_ID}")
private String appID;
@Value("${app_KEY}")
private String appKEY;
@Value("${redirect_URI}")
private String redirectURI;
@Value("${grant_type}")
private String grantType;
@Resource
private MemeberMapper memeber;
//根据code查询
public Member LoginBack(String code) {
Member member = new Member();
//获取accessToken
String urlAccessToken = HttpUtil.get("https://graph.qq/oauth2.0/token?grant_type=" + grantType + "&client_id=" + appID + "&client_secret=" + appKEY + "&code=" + code + "&redirect_uri=" + redirectURI);
//截取accessToken
String accessToken = StrUtil.subBetween(urlAccessToken, "access_token=", "&expires_in");
if(!StrUtil.isBlank(accessToken)){
//获取openId
String urlOpenId = HttpUtil.get("https://graph.qq/oauth2.0/me?access_token="+accessToken);
//截取openId
String openId = StrUtil.subBetween(urlOpenId, "openid\":\"", "\"}");
if(!StrUtil.isBlank(openId)){
try {
//访问用户资料
String userMessage = HttpUtil.get("https://graph.qq/user/get_user_info?access_token=" + accessToken + "&oauth_consumer_key="+appID+"&openid=" + openId);
JSONObject jsonObject = JSONUtil.parseObj(userMessage);
//获取用户姓名
String nickname = (String)jsonObject.get("nickname");
//获取QQ头像
String nickImg = (String)jsonObject.get("figureurl_qq_1");
//获取QQ性别
String gender= (String)jsonObject.get("gender");
//这里的随便怎么写
}catch (Exception e){
System.out.println(e);
}
}
}
return member;
}
}
说一下QQ的执行流程吧
https://wiki.connect.qq/%E4%BD%BF%E7%94%A8authorization_code%E8%8E%B7%E5%8F%96access_token
主要的执行流程:
QQ页面登录后 ===> 会往回调函数里面穿一个code值, => 根据code值获取accessToken值 =>再根据accessToken值>获取openId值
再拿openId值去访问接口获取基本信息,这个我得注释写的很清楚了哈,具体的流程就是这样
问题一:,填了回调接口以下,只能回调到服务器上,回调不到本地?
答案:使用 第三方的映射工具,把本地的服务映射成域名.就可以.我在这里用的是小米球映射工具
问题二:QQ登录界面出现回调地址校验通知错误?
答案:在本地电脑上host目录里面配置一下
版权声明:本文标题:网站接入QQ登录最新2020 java版本 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1729305173a1195013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论