QQ login for third-party login

1. Enter Tencent QQ website http://connect.qq.com/ to apply for APP KEY (not explained here)
to get

APP ID
APP KEY
2. Download SDK (http://qzonestyle.gtimg.cn/qzone/vas /opensns/res/doc/qqConnect_Server_SDK_java_v2.0.zip)
decompress and put Sdk4J.jar in your lib directory, put the qqconnectconfig.properties file in the source directory
Modify
app_ID = 100**** in this file (please modify this APP ID applied for you at here)
app_KEY = ******(Please modify the APP KEY applied for you here)
redirect_URI = ***********(Please modify here to log in After successful request uri)
3 Put <a href='/qq-login.html'> on the page and click here to send the background request,
        @RequestMapping(method=RequestMethod.GET,value="qq-login")
public String qqLogin (HttpServletRequest request){
String url = null;
try {
url = new Oauth().getAuthorizeURL(request);
} catch (QQConnectException e) {
e.printStackTrace();
}
return "redirect:" + url;
} The
        request jumps to the QQ authorization page. After the authorization is successful, it will return our configuration redirect_URI request,
        @RequestMapping(method=RequestMethod.GET,value ="qq-redirect")
public String qqRedirect(){
return "redirect:main.html";
}
        Here we have successfully logged in and returned to our own page.
       This is not enough, because we also need to get the information that QQ returns to us. Please see below,
      I wrote the following method
      try {
AccessToken accessTokenObj = new Oauth().getAccessTokenByRequest(request);
if(StringUtils.isBlank(accessTokenObj.getAccessToken())){
throw new ServiceException("QQ login failed") ;
}
String token = accessTokenObj.getAccessToken();
OpenID openIdObj = new OpenID(token);
String openID = openIdObj.getUserOpenID();
com.qq.connect.api.qzone.UserInfo qzoneUserInfo = new com.qq.connect.api.qzone.UserInfo(token, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
if(userInfoBean.getRet() == 0) {
UserInfo userInfo = new UserInfo();
userInfo.setUserName(userInfoBean.getNickname());
userInfo.setSex(userInfoBean.getGender());
userInfo.setIcon(userInfoBean.getAvatar().getAvatarURL50());
com.qq.connect.api.weibo.UserInfo weiboUserInfo = new com.qq.connect.api.weibo.UserInfo(accessToken, openID);
        com.qq.connect.javabeans.weibo.UserInfoBean weiboUserInfoBean = weiboUserInfo.getUserInfo();
        if (weiboUserInfoBean.getRet() == 0) {
        Calendar cal = Calendar.getInstance();
        cal.set(weiboUserInfoBean.getBirthday().getYear(), weiboUserInfoBean.getBirthday().getMonth(), weiboUserInfoBean.getBirthday().getDay());
        userInfo.setBirthday(cal.getTime());
             //System.out.println("<p>所在地:" + weiboUserInfoBean.getCountryCode() + "-" + weiboUserInfoBean.getProvinceCode() + "-" + weiboUserInfoBean.getCityCode()
             //         + weiboUserInfoBean.getLocation());
         }
}

}
throw new ServiceException(userInfoBean.getMsg());
} catch (QQConnectException e) {
throw new ServiceException(e.getMessage());
}

We got the QQ information because we can't get the QQ number, but we can identify it through openID.
If you don't understand some examples in qqConnect_Server_SDK_java_v2.0.zip, you can take a look.


Attached (There was a small episode in the process of applying for the app key. Because the QQ nickname was not displayed on the request page that was successfully returned by the authorization, it was not approved for me, but it was prompted that the login icon was placed incorrectly. Finally, I asked customer service. I know this is not the problem, I hope people will not take detours in the future)

Welcome to Zhouwangpu http://www.zhouwangpu.com/login.html to view the example

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327030721&siteId=291194637