一,在qq开放平台上申请开发者,得到APP ID和APP KEY
二,在qq开放平台上下载自己需要的SDK,http://wiki.connect.qq.com/sdk%E4%B8%8B%E8%BD%BD
三,写代码,把SDK中的架包(Sdk4J.jar)引导你的项目中去,qqconnectconfig.properties配置文件中,app_ID,app_KEY是你前面申请得到的,redirect_URI是授全成功时候返回的页面
四,请求授权登录 你在页面放置qq登录的按钮,点击按钮调用你的程序,String url = new Oauth().getAuthorizeURL(request); 去配置文件qqconnectconfig.properties中查找相对应的参数,然后拼起来去请求qq平台,它会返回一个授权登录的页面
五,授权页面会自动检测你的电脑上是否登陆qq了,如果有,直接让你快速登陆,如果没有,输入账号密码,然后授权。授权成功了,它会跳转到你的redirect_URI给的页面,这个时候你可以做很多事情了,获取用户的数据(做你需要的事情),。。。。。。
代码如下:(架包里面的AfterLoginRedirectServlet.java)
package com.qq.connect.demo;
import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.PageFans;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.PageFansBean;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.javabeans.weibo.Company;
import com.qq.connect.oauth.Oauth;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
/**
* Date: 12-12-4
* Time: 下午4:36
*/
public class AfterLoginRedirectServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
try {
AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);
String accessToken = null,
openID = null;
long tokenExpireIn = 0L;
if (accessTokenObj.getAccessToken().equals("")) {
// 我们的网站被CSRF攻击了或者用户取消了授权
// 做一些数据统计工作
System.out.print("没有获取到响应参数");
} 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();
out.println("欢迎你,代号为 " + openID + " 的用户!");
request.getSession().setAttribute("demo_openid", openID);
out.println("<a href=" + "/shuoshuoDemo.html" + " target=\"_blank\">去看看发表说说的demo吧</a>");
// 利用获取到的accessToken 去获取当前用户的openid --------- end
out.println("<p> start -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- start </p>");
UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
out.println("<br/>");
if (userInfoBean.getRet() == 0) {
out.println(userInfoBean.getNickname() + "<br/>");
out.println(userInfoBean.getGender() + "<br/>");
out.println("黄钻等级: " + userInfoBean.getLevel() + "<br/>");
out.println("会员 : " + userInfoBean.isVip() + "<br/>");
out.println("黄钻会员: " + userInfoBean.isYellowYearVip() + "<br/>");
out.println("<image src=" + userInfoBean.getAvatar().getAvatarURL30() + "/><br/>");
out.println("<image src=" + userInfoBean.getAvatar().getAvatarURL50() + "/><br/>");
out.println("<image src=" + userInfoBean.getAvatar().getAvatarURL100() + "/><br/>");
} else {
out.println("很抱歉,我们没能正确获取到您的信息,原因是: " + userInfoBean.getMsg());
}
out.println("<p> end -----------------------------------利用获取到的accessToken,openid 去获取用户在Qzone的昵称等信息 ---------------------------- end </p>");
out.println("<p> start ----------------------------------- 验证当前用户是否为认证空间的粉丝------------------------------------------------ start <p>");
PageFans pageFansObj = new PageFans(accessToken, openID);
PageFansBean pageFansBean = pageFansObj.checkPageFans("97700000");
if (pageFansBean.getRet() == 0) {
out.println("<p>验证您" + (pageFansBean.isFans() ? "是" : "不是") + "QQ空间97700000官方认证空间的粉丝</p>");
} else {
out.println("很抱歉,我们没能正确获取到您的信息,原因是: " + pageFansBean.getMsg());
}
out.println("<p> end ----------------------------------- 验证当前用户是否为认证空间的粉丝------------------------------------------------ end <p>");
out.println("<p> start -----------------------------------利用获取到的accessToken,openid 去获取用户在微博的昵称等信息 ---------------------------- start </p>");
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) {
//获取用户的微博头像----------------------start
out.println("<image src=" + weiboUserInfoBean.getAvatar().getAvatarURL30() + "/><br/>");
out.println("<image src=" + weiboUserInfoBean.getAvatar().getAvatarURL50() + "/><br/>");
out.println("<image src=" + weiboUserInfoBean.getAvatar().getAvatarURL100() + "/><br/>");
//获取用户的微博头像 ---------------------end
//获取用户的生日信息 --------------------start
out.println("<p>尊敬的用户,你的生日是: " + weiboUserInfoBean.getBirthday().getYear()
+ "年" + weiboUserInfoBean.getBirthday().getMonth() + "月" +
weiboUserInfoBean.getBirthday().getDay() + "日");
//获取用户的生日信息 --------------------end
StringBuffer sb = new StringBuffer();
sb.append("<p>所在地:" + weiboUserInfoBean.getCountryCode() + "-" + weiboUserInfoBean.getProvinceCode() + "-" + weiboUserInfoBean.getCityCode()
+ weiboUserInfoBean.getLocation());
//获取用户的公司信息---------------------------start
ArrayList<Company> companies = weiboUserInfoBean.getCompanies();
if (companies.size() > 0) {
//有公司信息
for (int i=0, j=companies.size(); i<j; i++) {
sb.append("<p>曾服役过的公司:公司ID-" + companies.get(i).getID() + " 名称-" +
companies.get(i).getCompanyName() + " 部门名称-" + companies.get(i).getDepartmentName() + " 开始工作年-" +
companies.get(i).getBeginYear() + " 结束工作年-" + companies.get(i).getEndYear());
}
} else {
//没有公司信息
}
//获取用户的公司信息---------------------------end
out.println(sb.toString());
} else {
out.println("很抱歉,我们没能正确获取到您的信息,原因是: " + weiboUserInfoBean.getMsg());
}
out.println("<p> end -----------------------------------利用获取到的accessToken,openid 去获取用户在微博的昵称等信息 ---------------------------- end </p>");
}
} catch (QQConnectException e) {
}
}
}