web实现QQ第三方登录(java版)

4 篇文章 0 订阅

简介:

主要逻辑在java实现,h5只有一个a标签调用接口。

后台分两个接口:

接口1:登录前缀接口,h5负责调用唤醒QQ授权。

接口2:需要在腾讯开放平台配置回调地址的接口,就是授权成功后回调。

java  controler代码:

package cn.network.controller;

import cn.network.exception.ValidataException;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Log4j2
@RequestMapping("/qq")
@RestController
public class LoginController {

    /**
     * 前端请求接口
     * 登录前缀
     * @param request
     * @param response
     * @throws QQConnectException
     * @throws IOException
     */
    @RequestMapping("/QQLogin")
    public void QQLogin(HttpServletRequest request, HttpServletResponse response) throws QQConnectException, IOException {
        String authorizeURL = new Oauth().getAuthorizeURL(request);
        log.info("authorizeURL》》》"+authorizeURL);
        response.sendRedirect(authorizeURL);
    }

    /**
     * 登录回调
     * 注意:需要在腾讯平台配置回调地址为改接口,本地地址无法测试回调
     * @param request
     * @return
     * @throws QQConnectException
     */
    @RequestMapping("/QQLoginCallbck")
    public Map<String,Object> getTroubleshootingList(HttpServletRequest request) throws QQConnectException {

        request.getSession().setAttribute("qq_connect_state", request.getParameter("state"));
        AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
        //我们的网站被CSRF攻击了或者用户取消了授权
        //做一些数据统计工作
        if (accessTokenObj.getAccessToken().equals("")) {
            throw new ValidataException("没有获取到响应参数");
        }
        log.info("没有获取到响应参数");
        String accessToken = accessTokenObj.getAccessToken();
        long tokenExpireIn = accessTokenObj.getExpireIn();
        log.info("accessToken》》》" +accessToken);
        log.info("tokenExpireIn》》》" +tokenExpireIn);
        // 利用获取到的accessToken 去获取当前用的openid -------- start
        OpenID openIDObj =  new OpenID(accessToken);
        String openID = openIDObj.getUserOpenID();
        log.info("openId》》》" + openID);
        // 利用获取到的accessToken 去获取当前用户的openid --------- end
        log.info("》》》start 利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息start");
        UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
        UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
        if (userInfoBean.getRet() != 0) {
            throw new ValidataException("很抱歉,我们没能正确获取到您的信息,原因是: " + userInfoBean.getMsg());
        }
        log.info("Nickname》》》"+userInfoBean.getNickname());
        log.info("gender》》》"+userInfoBean.getGender());
        log.info("黄钻等级: " + userInfoBean.getLevel() + "<br/>");
        log.info("会员 : " + userInfoBean.isVip() + "<br/>");
        log.info("黄钻会员: " + userInfoBean.isYellowYearVip() + "<br/>");
        log.info("<image src=" + userInfoBean.getAvatar().getAvatarURL30());
        log.info("<image src=" + userInfoBean.getAvatar().getAvatarURL50());
        log.info("<image src=" + userInfoBean.getAvatar().getAvatarURL100());
        log.info("》》》end利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息end");
        Map<String,Object> map=new HashMap<>();
        map.put("openId",openID);
        map.put("nickname",userInfoBean.getNickname());
        map.put("gender",userInfoBean.getGender());
        map.put("avatarUrl",userInfoBean.getAvatar().getAvatarURL100());
        return map;
    }

}

java 配置文件代码:

注意:需要在resources目录下新建一个qqconnectconfig.properties文件代码如下:

app_ID = 需要改成腾讯开放平台应用的APPID
app_KEY = 需要改成腾讯开放平台应用的APPKey
redirect_URI = http://127.0.0.1:8888//emotion-app/qq/QQLoginCallbck
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.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
addTURL = 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

腾讯SDK:

 

Sdk4J.jar

网盘地址:https://pan.baidu.com/s/1c1WDhfLMElUeoUxN9FpW-Q

提取码:zfma

h5代码:

  <a href="http://127.0.0.1:8888//emotion-app/qq/QQLogin">请使用你的QQ账号登陆</a>

有问题请加QQ:1907377985

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值