美文网首页
qq登入开发

qq登入开发

作者: 吴繁飞 | 来源:发表于2019-05-15 15:11 被阅读0次

点这里:https://blog.csdn.net/pan_y95/article/details/77916579
点这里:http://www.360doc.com/content/16/1123/15/14264700_608809855.shtml

package com.lianwei.util;

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 javax.servlet.http.HttpServletRequest;

public class QQUtils {
    /**
     * 利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息
     * @param request
     * @return
     */
    public static UserInfoBean createUserInfo(HttpServletRequest request){
        UserInfoBean userInfoBean=null;
        try {

            String accessToken   = null,
                    openID        = null;
            long tokenExpireIn = 0L;

            // 第一步-- 得到token类
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
            if (accessTokenObj.getAccessToken().equals("")) {
                //我们的网站被CSRF攻击了或者用户取消了授权
                //做一些数据统计工作
                System.out.println("没有获取到响应参数");
            } else {
                accessToken = accessTokenObj.getAccessToken();
                tokenExpireIn = accessTokenObj.getExpireIn();

                request.getSession().setAttribute("demo_access_token", accessToken);
                request.getSession().setAttribute("demo_token_expirein", String.valueOf(tokenExpireIn));

                // 利用获取到的accessToken 去获取当前用的openid -------- start
                OpenID openIDObj =  new OpenID(accessToken);
                openID = openIDObj.getUserOpenID();

                System.out.println("欢迎你,代号为 " + openID + " 的用户!");
                request.getSession().setAttribute("demo_openid", openID);
//                    // 利用获取到的accessToken 去获取当前用户的openid --------- end


                System.out.println("<p> start -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- start </p>");
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
                userInfoBean = qzoneUserInfo.getUserInfo();
                if (userInfoBean.getRet() == 0) {
                    System.out.println(userInfoBean.getNickname() + "<br/>");
                    System.out.println(userInfoBean.getGender() + "<br/>");
                } else {
                    System.out.println("很抱歉,我们没能正确获取到您的信息,原因是: " + userInfoBean.getMsg());
                }
                System.out.println("<p> end -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- end </p>");

            }
        } catch (QQConnectException e) {
        }

        return userInfoBean;
    }




}

log4j.rootLogger=debug,stdout,logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG


   //qq登入
    @RequestMapping("/qq")
    public void list(HttpServletResponse response, HttpServletRequest request) throws IOException, QQConnectException {
        response.setContentType("text/html;charset=utf-8");
        //一定要用这句话去弹出qq登录窗口,否则AccessToken会提示没有获取到响应参数
        response.sendRedirect(new Oauth().getAuthorizeURL(request));
    }
   // 申请 网站回调域-到后台的方法
    @RequestMapping("/qqhou")
    public String showIndexHtml(String code,String state,HttpServletRequest request,HttpServletResponse response) throws IOException, QQConnectException{

        UserInfoBean user= QQUtils.createUserInfo(request);
        System.out.println("user.getNickname()= "+user.getNickname());
   //     System.out.println(request.getSession().getAttribute("demo_openid"));
        return "qq";
    }

相关文章

网友评论

      本文标题:qq登入开发

      本文链接:https://www.haomeiwen.com/subject/nlipaqtx.html