第三方qq登录接口

QQ第三方登录
1.首先就是去QQ互联登录申请了,为什么要申请呢,是应为他会给你分配一个appid和一个appkey给,不然你是没有资格去第三方登录的,申请大概一个星期左右

这里写图片描述**
唯一需要注意的是第二个 回调地址了 这个是可以随时修改的

2.获取到了appid和appkey然后我们就需要开发后台了
我们先去获取java sdk 记得是java sdk不是js sdk
java SDk 下载
解压:

这里写图片描述
然后将jar包放入自己的项目

有兴趣的可以读一下 ReadMe.txt 了解更详细 我们继续下一步
打开qqconnectconfig.properties

app_ID = 100****(请修改此处)
app_KEY = ******(请修改此处)
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_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

上面是需要修改四处的根据自己的信息来修改
app_ID 申请下来的 appid
app_KEY 申请下来的 appkey
redirect_URI 自己申请时候填写的回调地址 所以说
scope
这里写图片描述
我们一般的话第一个就可以 会获取到qq的名称性别和头像,如果需要其他的话也可以自己去申请 。你自己有什么权限的你就可以早scope后面添加

到这里我们的准备工作就准备好了,开始直接测试吧,
先测试一下自己的接口能不能用

<a href="<%=path%>/qq/QQlogin">请使用你的QQ账号登陆</a> 
<a href="<%=path%>/qq/afterlogin">回调的测试</a>
@RequestMapping("/qq/QQlogin")
    protected void doGet(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        try {
           /* String authorizeURL = new Oauth().getAuthorizeURL(request);*/
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (QQConnectException e) {
            e.printStackTrace();
        }
    }

new Oauth().getAuthorizeURL(request) jar包提供的方法哦 会读取你在qqconnectconfig.properties 里面的信息 然后拼接出来成一URL,具体是什么样的可以去官方文档去看,
这里写图片描述

授权的话就会进去自己在申请时候设置的回调了(redirect_URI)

 @RequestMapping("/qq/afterlogin")
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
            try {
                AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
                String accessToken   = null,
                        openID        = null;
                long tokenExpireIn = 0L;
                if (accessTokenObj.getAccessToken().equals("")) {
                    System.out.print("没有获取到响应参数");
                }else{
                    accessToken = accessTokenObj.getAccessToken();
                    tokenExpireIn = accessTokenObj.getExpireIn();
                    OpenID openIDObj =  new OpenID(accessToken);
                    openID = openIDObj.getUserOpenID();
                    UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                    UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return null;
        }

这样就可以获取到信息了,这里所有的方法都是已经封装好的,下载好sdk就可以获取了,写的很简单,但是绝对没那么简单,最基本的登录,有兴趣课可以去看官方的详细文档
http://wiki.connect.qq.com/%E4%BD%BF%E7%94%A8authorization_code%E8%8E%B7%E5%8F%96access_token

谢谢!

猜你喜欢

转载自blog.csdn.net/qq_40680694/article/details/81740709
今日推荐