net.gplatform
Sdk4J
2.0
最好查看qq给出的demo例子
创建 qqconnectconfig.properties放在resources根目录下
redirect_URI = http://javatv.cn/loginAfter为登录之后重定向跳转的地址
app_ID = 这里填域名id
app_KEY = 这里填域名的key
redirect_URI = http://域名/loginAfter
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
public class QQcontroller {
@Autowired
private RestTemplate restTemplate;
@Autowired
private UserService userService;
@RequestMapping("/qqLogin")
public void qqLogin(HttpServletRequest request, HttpServletResponse response){
// response.setContentType("text/html;charset=utf-8");
try {
//重定向到qq的授权登录页面
response.sendRedirect(new Oauth().getAuthorizeURL(request));
} catch (Exception e) {
e.printStackTrace();
}
}
/***
* 登录成功之后,它会自动跳转到域名http://javatv.cn/loginAfter
* 需要配置hosts文件, 127.0.0.1 javatv.cn才能跳转到这里
*/
@RequestMapping("/loginAfter")
public String afterLogin(HttpServletRequest request, HttpServletResponse response){
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();
System.out.println("欢迎你,代号为 " + openID + " 的用户!");
request.getSession().setAttribute("demo_openid", openID);
System.out.println("去看看发表说说的demo吧");
// 利用获取到的accessToken 去获取当前用户的openid --------- end
/***
* 自己的代码:获取qq信息
*/
System.out.println("==================start====================");
String url = "https://graph.qq.com/user/get_user_info?access_token="
+ accessToken + "&oauth_consumer_key=101543517" + "&openid=" + openID + "&format=json ";
String json = restTemplate.getForObject(url, String.class);
//反序列化
ObjectMapper objectMapper = new ObjectMapper();
HashMap map = objectMapper.readValue(json, HashMap.class);
System.err.println(map);
System.err.println(map.get("figureurl_qq_1"));
/***
* 如果用户存在openId,说明用户之前登录过了,直接跳转到main.jsp
* 如果用户不存在,那么就说明用户第一次登录,需要保存到数据库,然后在跳转到main.jsp
*/
User user = new User();
user.setOpenid(openID);
User u = userService.queryByParam(user);
if (u!=null){
return "main";
}else {
user.setLoginName(UUID.randomUUID().toString());
user.setLoginPwd("123456");
user.setDelFlag(1);
userService.insert(user);
}
System.out.println("==================end====================");
System.out.println(" start ----------------------------------- 验证当前用户是否为认证空间的粉丝------------------------------------------------ start
");
PageFans pageFansObj = new PageFans(accessToken, openID);
PageFansBean pageFansBean = pageFansObj.checkPageFans("97700000");
if (pageFansBean.getRet() == 0) {
System.out.println("
验证您" + (pageFansBean.isFans() ? "是" : "不是") + "QQ空间97700000官方认证空间的粉丝
");
} else {
System.out.println("很抱歉,我们没能正确获取到您的信息,原因是: " + pageFansBean.getMsg());
}
}
} catch (Exception e){
e.printStackTrace();
}
return "main";
}
}
<%--第三方登录--%>
//qq登录点击事件
$("#qqLogin").click(function () {
location.href="/qqLogin"
})
C:\windows\system32\drivers\etc
输入javatv.cn的时候跳转到127.0.0.1
127.0.0.1 javatv.cn