提醒:为方便审核通过,建议在网站明细位置放置QQ按钮,先做好相关的页面跳转,逻辑可以先不写,再提交审核
必要条件申请app_ID,app_KEY
第一次申请需要提交开发者相关资料,通过后创建应用,PC端创建网站应用,填写网站地址,回调地址,网站名称,网站备案号,其中回调地址是QQ登录成功后返回的地址,在这个地址我们可以获取用户昵称,openid等,这里我们填写网站地址/plugin/QqLoginAfter
比如见鬼网的
https://faceghost.com/plugin/QqLoginAfter
审核通过后如下:
这里我们下载SDK for 网站接入 的Java SDK v2.0
下载完成后将解压后的Sdk4J.jar添加到项目lib目录下
项目的classpath新增qqconnectconfig.propertiesapp_ID = 你的id
app_KEY = 你的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创建2个sevlet,第一个用于跳转到QQ授权页面,一个用于授权成功后回调
参考如下:
qqLogin
com.faceghost.fg.plugin.QqLogin
qqLoginAfter
com.faceghost.fg.plugin.QqLoginAfter
qqLogin
/plugin/QqLogin
qqLoginAfter
/plugin/QqLoginAfter
处理相关逻辑
QqLogin.class 跳转到QQ授权,直接调用jar下的方法即可
参考:package com.faceghost.fg.plugin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.qq.connect.QQConnectException;
import com.qq.connect.oauth.Oauth;
public class QqLogin extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.sendRedirect(new Oauth().getAuthorizeURL(request));
} catch (QQConnectException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
QqLoginAfter.class 回调地址,处理逻辑,获取用户昵称,openid;
参考:package com.faceghost.fg.plugin;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.faceghost.fg.service.statics.LoginPluginStatic;
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;
public class QqLoginAfter extends HttpServlet {
private Logger log = Logger.getLogger(QqLoginAfter.class);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
try {
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
String accessToken = null,
openID = null;
if (accessTokenObj.getAccessToken().equals("")) {
response.sendRedirect(basePath);
} else {
accessToken = accessTokenObj.getAccessToken();
OpenID openIDObj = new OpenID(accessToken);
openID = openIDObj.getUserOpenID();
log.info("QQ openId:" + openID);
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
if (userInfoBean.getRet() == 0) {
try {
log.info("QQ nickname:" + userInfoBean.getNickname());
/**
*
* 这里,就需要你处理自己的逻辑了,首先你已经获取了QQ的openid,这个是跟QQ一一对应的,
*
* 在你的数据库创建一张表,里面保存,网站登录帐号ID,openid ,
*
* 用户在进行使用QQ登录时,根据openid去表中获取帐号ID,
* 如果不存在,跳转到绑定页面,进行保存数据库,然后调用登录逻辑,进行登录。
* 如果存在,在数据查询该openid关联的帐号ID, 然后调用登录逻辑,进行登录。
*
*/
} catch (Exception e) {
e.printStackTrace();
}
}else{
response.sendRedirect(basePath);
}
}
} catch (QQConnectException e) {
}
}
}使用QQ登录