java 实现微博,QQ联合登录
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt313
开发平台
http://connect.qq.com/
http://open.weibo.com/
可以下载相应的sdk 和 API
Java代码
- 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
package
com.fanbaobao.action;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.HttpSession;
import
com.fanbaobao.service.FbbUser;
import
com.fanbaobao.service.FbbService.Client;
import
com.fanbaobao.util.FbbUtil;
import
weibo4j.Oauth;
import
weibo4j.Users;
import
weibo4j.Weibo;
import
weibo4j.http.AccessToken;
import
weibo4j.model.User;
public
class
SinaAction
extends
BaseAction {
private
String errorMsg;
public
String toLogin()
throws
Exception{
try
{
HttpServletRequest request=getRequest();
Oauth oauth=
new
Oauth();
getResponse().sendRedirect(oauth.authorize(
"code"
));
//重定向到新浪授权页面
}
catch
(Exception e) {
e.printStackTrace();
}
return
null
;
}
/**
* 新浪微博登录的回调
* @return
* @throws Exception
*/
public
String doLogin()
throws
Exception{
HttpServletRequest request=getRequest();
HttpSession session=request.getSession();
String accesstoken =
null
;
String code=request.getParameter(
"code"
);
if
(code!=
null
)
{
Oauth oauth=
new
Oauth();
AccessToken accessToken=oauth.getAccessTokenByCode(code);
accesstoken=accessToken.getAccessToken();
if
(accessToken!=
null
)
{
Weibo weibo=
new
Weibo();
weibo.setToken(accessToken.getAccessToken());
Users users=
new
Users();
User weiboUser=users.showUserById(accessToken.getUid());
HttpServletResponse response=getResponse();
System.out.println(weiboUser.getId());
System.out.println(weiboUser.getGender());
System.out.println(weiboUser.getName());
System.out.println(accesstoken);
// if(first<2){
// if(first==1){ //用户第一次登录,发一条微博,且follow 51bi
// Timeline timeline=new Timeline();
// timeline.UpdateStatus("我注册成为了#比购网#会员,免费注册后去淘宝购物,最高有35%的现金返还。最给力的是:到拉手、京东商城、1号店等400多家知名商城不仅能享受最低价格,还能额外省钱40%。喜欢网购的童鞋都去注册一个省钱吧!官方网址:http://www.51bi.com/space/biuser/register.jsp?currentUrl=http://www.51bi.com/taobao/");
// try {
// Friendships fm = new Friendships();
// fm.createFriendshipsById("1718952754");//follow 51bi
// fm.createFriendshipsById("2908640880");//follow 51bi
// } catch (Exception e) {
// }
// }
//未完善资料
Client client = FbbUtil.getClient();
FbbUser fbbUser= client.doOauth(
1
, weiboUser.getId());
session.setAttribute(
"fbbUserInfo"
, fbbUser);
return
SUCCESS;
}
}
else
{
errorMsg=
"新浪oauth 认证请求非法!"
;
return
LOGIN;
}
return
SUCCESS;
}
public
String getErrorMsg() {
return
errorMsg;
}
public
void
setErrorMsg(String errorMsg) {
this
.errorMsg = errorMsg;
}
}
Java代码
- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
package
com.fanbaobao.action;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
javax.servlet.http.HttpSession;
import
com.fanbaobao.service.FbbUser;
import
com.fanbaobao.service.FbbService.Client;
import
com.fanbaobao.util.FbbUtil;
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.oauth.Oauth;
public
class
QzoneAction
extends
BaseAction {
private
String errorMsg;
public
String toLogin()
throws
Exception{
this
.getResponse().setContentType(
"text/html;charset=utf-8"
);
try
{
this
.getResponse().sendRedirect(
new
Oauth().getAuthorizeURL(
this
.getRequest()));
}
catch
(QQConnectException e) {
e.printStackTrace();
}
return
null
;
}
public
String doLogin()
throws
Exception{
HttpServletResponse response=
this
.getResponse();
HttpServletRequest request=
this
.getRequest();
HttpSession session=request.getSession();
response.setContentType(
"text/html; charset=utf-8"
);
try
{
AccessToken accessTokenObj =
(
new
Oauth()).getAccessTokenByRequest(request);
String accessToken =
null
,
openID =
null
;
Long tokenExpireIn =
null
;
if
(accessTokenObj.getAccessToken().equals(
""
)) {
errorMsg=
"QQ登录校验失败!"
;
return
LOGIN;
}
else
{
accessToken = accessTokenObj.getAccessToken();
tokenExpireIn = accessTokenObj.getExpireIn();
request.getSession().setAttribute(
"demo_access_token"
, accessToken);
request.getSession().setAttribute(
"demo_token_expirein"
, String.valueOf(tokenExpireIn));
OpenID openIDObj =
new
OpenID(accessToken);
openID = openIDObj.getUserOpenID();
request.getSession().setAttribute(
"demo_openid"
, openID);
UserInfo qzoneUserInfo =
new
UserInfo(accessToken, openID);
UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();
//
PageFans pageFansObj =
new
PageFans(accessToken, openID);
PageFansBean pageFansBean = pageFansObj.checkPageFans(
"97700000"
);
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();
// 第三方处理用户绑定逻辑
String user_id=openID;
String nickname=
new
String(userInfoBean.getNickname());
System.out.println(user_id);
System.out.println(nickname);
Client client = FbbUtil.getClient();
FbbUser fbbUser= client.doOauth(
2
, user_id);
session.setAttribute(
"fbbUserInfo"
, fbbUser);
return
SUCCESS;
// session.setAttribute("oauth_token", oauth_token);
// session.setAttribute("oauth_token_secret", oauth_token_secret);
// InterUser interUser=new InterUser(
Constants.LOGIN_FROM_QQ,user_id,
null
,nickname);
// interUser.setBiUserService(biUserService);
// interUser.setRuledao(ruledao);
// interUser.setAccesstoken(accessToken);
// if (tokenExpireIn!=null) {
// interUser.setExpireIn(String.valueOf(tokenExpireIn));
// }
// session.setAttribute("uid", user_id);
// session.setAttribute("fromsite", Constants.LOGIN_FROM_QQ);
// int first=interUser.doUserLogin(request,response,false); //接口登录
// if(first<2){
// session.setAttribute("loginname",nickname);
// if (first==-1) {
// session.setAttribute("msg","save userBind error");
// }else if (first==-2) {
// session.setAttribute("msg","error");
// }
// return "qzone";
// }
}
}
catch
(QQConnectException e) {
e.printStackTrace();
return
LOGIN;
}
}
public
String getErrorMsg() {
return
errorMsg;
}
public
void
setErrorMsg(String errorMsg) {
this
.errorMsg = errorMsg;
}
}
Java代码
//login.properties
############
#t.sina login
###########
client_ID =xx
client_SERCRET =xx
redirect_URI =xxx
baseURL=https://api.weibo.com/2/
accessTokenURL=https://api.weibo.com/oauth2/access_token
authorizeURL=https://api.weibo.com/oauth2/authorize
############
#taoBao login
###########
#TAOBAO_KEY=xxxx
#TAOBAO_SECRET=xxxxxx
#TAOBAO_REDIRECT=xxxx
#authorizeURL=https://oauth.taobao.com/authorize
#accessTokenURL=https://oauth.taobao.com/token
//qqconnectconfig.properties
app_ID = x
app_KEY = xx
redirect_URI = xxx
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
转载于:https://www.cnblogs.com/grefr/p/6089075.html
java 实现微博,QQ联合登录相关推荐
- Java程序模拟QQ空间登录 - 并模拟刷说说的赞(图文) 注意:腾讯修改了加密算法,已失效(2015-01-31)
Java程序模拟QQ空间登录 - 并模拟刷说说的赞 背景:在2013-10-25的一天,有个friend在QQ问我:"post登陆QQ空间抓取不到post的数据".于是故事就开始了 ...
- Java Web 实现 QQ第三方登录
java web 实现QQ第三方登录功能 首先点击该链接进入腾讯开放平台 ,注册成为开发者 2.注册成功后到这个页面 3.创建web应用 4.填写基本信息(以前的这里会让你填写回调地址 ,现在改了) ...
- java Oauth2微博开放平台登录授权接入
--前期准备 微博开放平台登录账号,地址:https://open.weibo.com/ 提交身份认证(个人真实姓名.身份证号.身份证照片)审核需要3天左右 注册应用 填写应用名(选择网页应用) 注册 ...
- Java 实现第三方 QQ 账号登录
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | uidaima.com/blog/472561 ...
- Java笔记-模拟QQ三方登录(单点登录2.0)
本次例子模拟这样的情况: 一共有2个spring boot应用: 一个是某公司的web,第二个是QQ服务端. 某公司的应用为127.0.0.1:8082 QQ服务端为127.0.0.1:8081 演示 ...
- java协议手机qq批量登录软件_手把手分享:日常大量账号批量登录,批量点击软件...
在日常的办公系统里,我们是经常会遇到很多账号依次手工登录,然后进入再做一些重复性的点击操作.这时,如是手工登录账号和密码,然后再退出这样循环,一定累的七上八下的.而你不妨可以借助一款叫"阿冲 ...
- QQ微信微博联合登录流程总结
第三方联合登录一般可以降低网站的获客成本,所以一般的网站都会做一些联合登录,常用的就是QQ.微信.微博了,下面简单的介绍一下这些联合登录的方法. 看了一号店的QQ登录源码可知,点击图标直接访问的是这个 ...
- java加按钮_如何从零开始对接第三方登录(Java版):QQ登录和微博登录
阅读本文约需要8分钟 大家好,我是你们的导师,我每天都会在这里给大家分享一些干货内容(当然了,周末也要允许老师休息一下哈).上次老师跟大家分享了HashMap和TreeMap的知识,今天跟大家分享下对 ...
- 如何从零开始对接第三方登录(Java版):QQ登录和微博登录
前言 个人网站最近增加了评论功能,为了方便用户不用注册就可以评论,对接了QQ和微博这2大常用软件的一键登录,总的来说其实都挺简单的,可能会有一点小坑,但不算多,完整记录下来方便后来人快速对接. 后台设 ...
最新文章
- 【神经网络】(11) 轻量化网络MobileNetV1代码复现、解析,附Tensorflow完整代码
- eslint 学习笔记
- 别拒绝生命中偶尔的初恋
- PHP数据库操作分页类
- web前端入门学习 css(8)(新增语义化标签、video/audio、新增input类型、新增表单属性、属性选择器、结构伪类选择器、伪元素选择器、css3盒子模型、模糊、calc函数、过渡
- JAVA实验——Java的基本程序设计结构
- LINUX之前UNIX的历史
- 经纬度 c代码中定义_如何将TXT文本格式的批量经纬度值导入到奥维成为标签
- P1351-联合权值【树形结构】
- 微信公众平台应用开发实战
- 资源描述框架RDF-阮一峰
- CentOS下搭建Jumpserver堡垒机
- 电脑win7语音怎么测试软件,win7话筒怎么测试 win7话筒测试方法【图文】
- 浅析指针(pointer)与引用(reference)
- 手动抛出异常_超实用:关于Java异常设计和处理
- java 核心API day05 File类
- 《寒江独钓windows内核安全编程》学习笔记之一
- 最新配资系统源码完美运行版+策略买点/A股系统
- 一个nginx使用同一端口不同域名代理到不同网站
- Linux线程同步与Windows线程同步