java 实现微博,QQ联合登录
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt313
开发平台
http://connect.qq.com/
http://open.weibo.com/
可以下载相应的sdk 和 API
Java代码
- 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
packagecom.fanbaobao.action;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.fanbaobao.service.FbbUser;importcom.fanbaobao.service.FbbService.Client;importcom.fanbaobao.util.FbbUtil;importweibo4j.Oauth;importweibo4j.Users;importweibo4j.Weibo;importweibo4j.http.AccessToken;importweibo4j.model.User;publicclassSinaActionextendsBaseAction {privateString errorMsg;publicString toLogin()throwsException{try{HttpServletRequest request=getRequest();Oauth oauth=newOauth();getResponse().sendRedirect(oauth.authorize("code"));//重定向到新浪授权页面}catch(Exception e) {e.printStackTrace();}returnnull;}/*** 新浪微博登录的回调* @return* @throws Exception*/publicString doLogin()throwsException{HttpServletRequest request=getRequest();HttpSession session=request.getSession();String accesstoken =null;String code=request.getParameter("code");if(code!=null){Oauth oauth=newOauth();AccessToken accessToken=oauth.getAccessTokenByCode(code);accesstoken=accessToken.getAccessToken();if(accessToken!=null){Weibo weibo=newWeibo();weibo.setToken(accessToken.getAccessToken());Users users=newUsers();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);returnSUCCESS;}}else{errorMsg="新浪oauth 认证请求非法!";returnLOGIN;}returnSUCCESS;}publicString getErrorMsg() {returnerrorMsg;}publicvoidsetErrorMsg(String errorMsg) {this.errorMsg = errorMsg;}}
Java代码
- 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
packagecom.fanbaobao.action;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.fanbaobao.service.FbbUser;importcom.fanbaobao.service.FbbService.Client;importcom.fanbaobao.util.FbbUtil;importcom.qq.connect.QQConnectException;importcom.qq.connect.api.OpenID;importcom.qq.connect.api.qzone.PageFans;importcom.qq.connect.api.qzone.UserInfo;importcom.qq.connect.javabeans.AccessToken;importcom.qq.connect.javabeans.qzone.PageFansBean;importcom.qq.connect.javabeans.qzone.UserInfoBean;importcom.qq.connect.oauth.Oauth;publicclassQzoneActionextendsBaseAction {privateString errorMsg;publicString toLogin()throwsException{this.getResponse().setContentType("text/html;charset=utf-8");try{this.getResponse().sendRedirect(newOauth().getAuthorizeURL(this.getRequest()));}catch(QQConnectException e) {e.printStackTrace();}returnnull;}publicString doLogin()throwsException{HttpServletResponse response=this.getResponse();HttpServletRequest request=this.getRequest();HttpSession session=request.getSession();response.setContentType("text/html; charset=utf-8");try{AccessToken accessTokenObj =(newOauth()).getAccessTokenByRequest(request);String accessToken =null,openID =null;Long tokenExpireIn =null;if(accessTokenObj.getAccessToken().equals("")) {errorMsg="QQ登录校验失败!";returnLOGIN;}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 =newOpenID(accessToken);openID = openIDObj.getUserOpenID();request.getSession().setAttribute("demo_openid", openID);UserInfo qzoneUserInfo =newUserInfo(accessToken, openID);UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();//PageFans pageFansObj =newPageFans(accessToken, openID);PageFansBean pageFansBean = pageFansObj.checkPageFans("97700000");com.qq.connect.api.weibo.UserInfo weiboUserInfo =newcom.qq.connect.api.weibo.UserInfo(accessToken, openID);com.qq.connect.javabeans.weibo.UserInfoBean weiboUserInfoBean =weiboUserInfo.getUserInfo();// 第三方处理用户绑定逻辑String user_id=openID;String nickname=newString(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);returnSUCCESS;// 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();returnLOGIN;}}publicString getErrorMsg() {returnerrorMsg;}publicvoidsetErrorMsg(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
java 实现微博,QQ联合登录的更多相关文章
- QQ联合登录(基于Oauth2.0协议)
1. 获取授权码Authorization Code https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id= ...
- 如何从零开始对接第三方登录(Java版):QQ登录和微博登录
前言 个人网站最近增加了评论功能,为了方便用户不用注册就可以评论,对接了QQ和微博这2大常用软件的一键登录,总的来说其实都挺简单的,可能会有一点小坑,但不算多,完整记录下来方便后来人快速对接. 后台设 ...
- 【Demo】QQ,github,微博第三方社交登录
本文主要讲解 集成 第三方社交账号登录 为什么会有这个需求? 主要是因为目前互联网的网站数量太多,如果在各个站点都注册一个账号 用户非常不容易记住每个账号的用户名和密码,并且非常难保证每个账号的密码足 ...
- 关于使用QQ、新浪微博、腾讯微博等第三方登录网站的开发过程(二)
(二).新浪微博登录 1. 首先在新浪微博开放平台注册成为开发者.[http://open.weibo.com/connect] 具体自己填写一些相关信息就OK! 2. 注册成功之后,点击[微连接], ...
- 微博爬虫“免登录”技巧详解及 Java 实现(业余草的博客)
一.微博一定要登录才能抓取? 目前,对于微博的爬虫,大部分是基于模拟微博账号登录的方式实现的,这种方式如果真的运营起来,实际上是一件非常头疼痛苦的事,你可能每天都过得提心吊胆,生怕新浪爸爸把你的那些账 ...
- QQ第三方登录
QQ第三方登录 在Android应用程序的开发过程中,很多时候需要加入用户登录/注册模块.除了自己动手设计登录界面并实现相应功能外,现在还可以借助百度.腾讯等开发者平台提供的第三方账号登录模块.最近研 ...
- 联合登录 & OAuth 2.0 & OpenID
联合登录 & OAuth 2.0 & OpenID 第三方联合登录一般可以降低网站的获客成本,所以一般的网站都会做一些联合登录,常用的就是QQ.微信.微博; https://www.z ...
- PHP实现QQ第三方登录
PHP实现QQ第三方登录 学习之前,请大家先看一下oAuth协议. 首先呢,我们进入QQ互联的官方网站 http://connect.qq.com登入我们自己的QQ号,没有QQ号的小伙伴可以忽略本篇博 ...
- QQ互联登录回调路径错误redirect uri is illegal(100010)
QQ互联登录设置的路径设置
随机推荐
- 【原创】-- C# 点滴积累 -- String
一.string.Format() 将[数字字符串]转为两位小数显示的字符串: dt = dtResult.Select(it => new CommodityPriceLimitEntity ...
- 双向lstm-crf源码的问题和细微修改
别人的源码地址:https://github.com/chilynn/sequence-labeling/ 如果你训练就会发现loss降到0以下,按照他设定的目标函数,loss理论上应该是大于0的,仔 ...
- strcpy.strcmp.strlen.strcat函数的实现
#include <stdio.h> #include <string.h> char *copy(char *a,char *b);//声明一个复制函数 char *ca ...
- Python爬虫从入门到放弃(二十)之 Scrapy分布式原理
关于Scrapy工作流程回顾 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享爬取队列. 分布式架 ...
- iframe嵌入页面不能全部展示
在嵌入页面不能全部展示的问题中,可以通过js改变iframe的高度 html部分代码: <iframe src="#" name="i" id=" ...
- PAT (Basic Level) Practise (中文) 1016. 部分A+B (15)
1016. 部分A+B (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 正整数A的"DA(为1 ...
- 计蒜客模拟赛D2T3 蒜头君救人:用bfs转移状压dp
题目链接:https://nanti.jisuanke.com/t/16444 题意: 蒜头君是一个乐于助人的好孩子,这天他所在的乡村发生了洪水,有多名村民被困于孤岛上,于是蒜头君决定去背他们离开困境 ...
- 17个新手常见Python运行时错误
当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , ...
- 设计模式之桥接模式(Bridge模式)
我想大家小时候都有用蜡笔画画的经历吧.红红绿绿的蜡笔一大盒,根据想象描绘出格式图样.而毛笔下的国画更是工笔写意,各展风采.而今天我们的故事从蜡笔与毛笔说起. 设想要绘制一幅图画,蓝天.白云.绿树.小鸟 ...
- mysql-索引与优化
写在前面:索引对查询的速度有着至关重要的影响,理解索引也是进行数据库性能调优的起点.考虑如下情况,假设数据库中一个表有10^6条记录,DBMS的页面大小为4K,并存储100条记录.如果没有索引,查询将 ...