2018最新QQ登录授权流程

第一步,去qq互联网站实名验证,创建应用,获取appid等参数。

 

第二步,copy下面的标签放到页面。


 

第三步,创建一个qqconnectconfig.properties(把****的地方替换成你创建应用获取的地址)

app_ID = ****
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

 

第四步,controller填写这几个方法。(注意,本controller的qq登录回调QQcallBack,就是第三步redirect_URL的地址。)

 /**
     * QQ登录跳转
     * @param request
     * @param response
     * @throws IOException
     */
    @RequestMapping("/qqLogin")
    public void qqLogin(HttpServletRequest request,HttpServletResponse response) throws IOException {
         response.setContentType("text/html;charset=utf-8");
        try {
            response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (QQConnectException e) {
            // TODO Auto-generated catch block
            new QQConnectException("跳转到QQ登录页面异常");
        }
    }
    
    /**
     * QQ登录回调
     * @param req
     * @param resp
     * @param session
     * @return
     * @throws QQConnectException
     */
    @RequestMapping(value="QQCallBack")
    public String qqcallback(HttpServletRequest request,HttpServletResponse response) throws QQConnectException{
        response.setContentType("text/html;charset=utf-8");
        AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
         String accessToken   = null;
         String openID  = null;
        try {
            accessToken = accessTokenObj.getAccessToken();
            request.getSession().setAttribute("accessToken", accessToken);
            if (StringUtil.isEmpty(accessToken)) {
                System.out.println("没有获取到响应参数");
            }else{
                OpenID openIDObj =  new OpenID(accessToken);
                openID=openIDObj.getUserOpenID();
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
                if(userInfoBean != null && userInfoBean.getRet() == 0 && StringUtil.isEmpty(userInfoBean.getMsg())){
                    System.out.println("欢迎你,代号为 " + openID + " 的用户!");
                    System.out.println("黄钻等级: " + userInfoBean.isYellowYearVip());
                    System.out.println("昵称: " + userInfoBean.getNickname());
                    System.out.println("性别: " + userInfoBean.getGender());
                    System.out.println("黄钻等级: " + userInfoBean.getLevel());
                    System.out.println("会员 : " + userInfoBean.isVip());
                    System.out.println("黄钻会员: " + userInfoBean.isYellowYearVip());
                    System.out.println("");
                    System.out.println("");
                    System.out.println("");
                    //获取用户成功
                    Blogger blogger=bloggerService.getByUserOpenId(openID);
                    
                    if(blogger!=null){//存在该用户
                        blogger.setNickName(userInfoBean.getNickname());
                        blogger.setUserName(userInfoBean.getNickname());
                        blogger.setPassword(CryptographyUtil.md5("123456", userInfoBean.getNickname()));
                        bloggerService.update(blogger);
                    }else{
                        blogger=new Blogger();
                        blogger.setNickName(userInfoBean.getNickname());
                        blogger.setOpenId(openID);
                        blogger.setSex(userInfoBean.getGender());
                        blogger.setImageName(userInfoBean.getAvatar().getAvatarURL100());
                        
                        blogger.setPassword(CryptographyUtil.md5("123456", userInfoBean.getNickname()));
                        blogger.setProfile(null);
                        blogger.setSign(null);
                        blogger.setUserName(userInfoBean.getNickname());
                        bloggerService.add(blogger);
                    }
                    request.getSession().setAttribute("qqblogger", blogger);
                    //存储用户信息
                    System.out.println("调用成功了");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/";
    }
    
    /**
      * 将emoji表情替换成空串
      *  
      * @param source
      * @return 过滤后的字符串
      */
     public static String filterEmoji(String source) {
      if (source != null && source.length() > 0) {
       return source.replaceAll("[\ud800\udc00-\udbff\udfff\ud800-\udfff]", "");
      } else {
       return source;
      }
     }

 

第五步,启动项目运行。

 

 

本站博客:www.wurao.xin

你可能感兴趣的:(2018最新QQ登录授权流程)