`
Luob.
  • 浏览: 1584315 次
  • 来自: 上海
社区版块
存档分类
最新评论

淘宝,新浪,QQ联合登录

阅读更多
开发平台
http://connect.qq.com/
http://open.taobao.com/
http://open.weibo.com/
可以下载相应的sdk 和 API

package com.fanbaobao.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import net.sf.json.JSONObject;

import com.bjdata.util.SecurityUtils;
import com.fanbaobao.service.FbbUser;
import com.fanbaobao.service.FbbService.Client;
import com.fanbaobao.util.FbbUtil;
import com.fanbaobao.util.MD5Util;
import com.sun.xml.internal.fastinfoset.stax.events.Util;
import com.taobao.api.internal.util.WebUtils;

public class TaobaoAction extends BaseAction {
	  private static final Logger logger = Logger.getLogger(TaobaoAction.class);
	  private String errorMsg;
	  public static String KEY = "xxx";  //沙箱环境key
	  public static String SECRET = "xxxx";
	  public static String REDIRECT="l";
	  public static String AUTHORIZEURL="";//沙箱环境
	  public static String TOKENURL=""; //沙箱环境
	  
	  public static String SAFEKEY="*(.com#$^&";
	  static{
	    	  try
		        {
		            Properties propert = new Properties();
		            java.io.InputStream in = TaobaoAction.class.getClassLoader().getResourceAsStream("login.properties");
		            propert.load(in);
		            KEY=propert.getProperty("TAOBAO_KEY",KEY);
		            SECRET=propert.getProperty("TAOBAO_SECRET",SECRET);
		            REDIRECT=propert.getProperty("TAOBAO_REDIRECT",REDIRECT);
		            AUTHORIZEURL=propert.getProperty("TAOBAO_AUTHORIZEURL",AUTHORIZEURL);
		            TOKENURL=propert.getProperty("TAOBAO_ACCESSTOKENURL",TOKENURL);
		        }
		        catch(Exception e)
		        {
		        }
	    	
	    }
	    
	    public void toLogin() throws IOException{
	    	StringBuilder url=new StringBuilder(AUTHORIZEURL);
	    	url.append("?response_type=code")
	    	.append("&client_id=").append(KEY)
	    	.append("&redirect_uri=").append(REDIRECT)
	    	.append("&state=").append(MD5Util.MD5Encode(SECRET+SAFEKEY+KEY, null));
			getResponse().sendRedirect(url.toString()); //重定向到新浪授权页面
	    }
	    
		public String doLogin() throws Exception{
			HttpServletRequest request=this.getRequest();
			String code=request.getParameter("code");
			String state=request.getParameter("state");
			logger.info("taobao doLogin state:"+state);
			if(Util.isEmptyString(code)){
				errorMsg="淘宝快捷登录授权失败,请重新授权!";
				return LOGIN;
			}
			Map<String, String> param = new HashMap<String, String>();
			param.put("grant_type", "authorization_code");
			param.put("code",code);
			param.put("client_id",KEY);
			param.put("client_secret",SECRET);
			param.put("redirect_uri",REDIRECT);
			param.put("view", "web");
			param.put("state",state);
			param.put("scope", "item"); //item,promotion,usergrade
			String responseJson=WebUtils.doPost(TOKENURL, param, 3000, 3000);
			JSONObject jo=JSONObject.fromObject(responseJson);
			if(jo.get("taobao_user_id")==null){
				errorMsg="淘宝快捷登录验证失败!";
				return LOGIN;
			}
			String user_id =jo.get("taobao_user_id").toString();
			String user_name=jo.get("taobao_user_nick").toString();
			HttpSession session=request.getSession();
			Client client = FbbUtil.getClient();
			FbbUser fbbUser= client.doOauth(3, user_id);
			session.setAttribute("fbbUserInfo", fbbUser);	
			return SUCCESS;
		}

		public String getErrorMsg() {
			return errorMsg;
		}

		public void setErrorMsg(String errorMsg) {
			this.errorMsg = errorMsg;
		}

}

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;
	}
}


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;
	}
}

//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

分享到:
评论
2 楼 vmvmvm80 2015-10-23  
大神能把全套代码发来不,最近要开发这个,又是新手一枚.参考官方的东西不懂怎么结合到自己的系统  443915350@163.com
1 楼 yl23250 2014-02-21  
大神,你能把全套发我么,主要是你那baseAction  谢谢拉  977628413@qq.com

相关推荐

    网站的 QQ联合登录 实例

    网站的 QQ联合登录 实例, 2012年最新技术,是网站验证身份的新形式。由腾讯便于2012.2月 正式推出,于7月左右变得列为稳定,期间,我与腾讯相关技术人员进行过多次沟通,助其发现最后一批BUG(有QQ聊天记录为证)。 ...

    android微信登录QQ登录新浪登录和分享

    本教程将详细讲解如何在Android应用中实现微信、QQ和新浪的登录以及内容分享功能。 1. **微信登录**: 微信开放平台提供了SDK供开发者接入。首先,你需要在微信开放平台注册并创建应用,获取AppID和AppSecret。...

    UCHOME 2.0新浪微博整合登录插件|QQ登录|人人网登录

    UCHOME 2.0作为一个流行的社区建站系统,提供了丰富的插件支持,其中就包括了对新浪微博、QQ和人人网等主流社交媒体平台的整合登录功能。这一功能使得用户无需注册新账号,可以直接使用已有的社交网络账号登录网站,...

    qq联合登录资料.7z

    下面我们将深入探讨QQ联合登录的工作原理、实现过程以及其在实际应用中的优势与挑战。 1. **工作原理**: QQ联合登录基于OAuth 2.0协议,这是一个开放标准授权框架,允许第三方应用在用户授权的情况下,获取用户的...

    EXshop支付宝 淘宝 新浪 QQ 快捷登陆插件

    总结起来,"EXshop支付宝 淘宝 新浪 QQ 快捷登陆插件"通过集成四大主流社交平台的登录接口,为EXshop电商平台提供了一种提升用户体验的有效方式。理解和掌握这些接口的使用,对于优化网站服务和增强用户黏性具有重要...

    python-QQ联合登录接口

    在实现Python QQ联合登录时,你需要以下几个关键步骤: 1. **申请QQ开放平台开发者账号**:首先,你需要在QQ开放平台上注册成为开发者,并创建一个应用,获取到AppID和AppKey,这两个是后续API调用的身份标识。 2....

    Android新浪微博和QQ登录ANE

    《Android新浪微博和QQ登录ANE详解》 在移动应用开发中,集成社交网络登录功能已经成为一个不可或缺的特性。本文将深入探讨如何在Android平台上实现新浪微博和QQ的登录功能,特别是通过ANE(Adobe Native Extension...

    第三方登录源码 集成了淘宝 qq 新浪微博

    通过分析这个资源,我们可以学习到如何在.NET环境中实现OAuth2协议,以便在自己的应用中集成淘宝、QQ、新浪微博等第三方登录。这对于想要提升用户体验、简化登录流程的开发者来说,是一份非常有价值的学习资料。

    UCHOME的QQ、新浪 api登录插件 技术文档

    《UCHOME的QQ、新浪API登录插件技术文档详解》 在互联网社交领域,用户账号的互联互通已经成为一种趋势,这极大地便利了用户的网络生活。UCHOME作为一款热门的社区建站系统,为了满足用户需求,提供了QQ和新浪的API...

    ios 第三方登陆 QQ登录 微信登录 新浪登录

    在iOS应用开发中,集成第三方登录服务,如QQ登录、微信登录和新浪登录,是提升用户体验和方便用户快速注册及登录的重要功能。标题中的“ios 第三方登陆 QQ登录 微信登录 新浪登录”指的是在iOS应用中实现这三种常见...

    html分享到新浪 QQ好友 QQ空间 腾讯微博

    html分享到新浪 QQ好友 QQ空间 腾讯微博

    第三方登陆 qq 新浪微博

    QQ和新浪微博都是中国流行的社交媒体平台,它们提供了开放的API接口,允许开发者集成第三方登录功能。下面我们将深入探讨这个主题。 1. **QQ互联(QQ Open Platform)**:QQ互联是腾讯公司提供的一项服务,它允许...

    网钛ASP版QQ登录与新浪微博登录插件 V1.0.rar

    1.该程序整合了QQ登录和新浪微博登录功能,后续会不断完善和增加其他登录方式。 2.该功能是从网钛文章管理系统(OTCMS)中剥离出来的。 版权声明: 1.网钛文章管理系统(OTCMS)QQ登录API接口修改于: 一曲小诗...

    ECMALL QQ支付宝 新浪 快捷登录

    ECMALL QQ支付宝 新浪 快捷登录是一种集成QQ、支付宝和新浪等第三方登录服务的解决方案,方便用户在电子商务平台(ECMALL)上快速注册和登录。这种集成方案简化了用户账号创建和验证的过程,提高了用户体验。以下是...

    腾讯QQ、新浪微博一键登录源码

    【腾讯QQ、新浪微博一键登录源码】是一种便捷的用户身份验证解决方案,它允许用户通过他们的腾讯QQ或新浪微博账号快速登录到第三方网站,而无需创建新的账户或记住额外的用户名和密码。这种技术大大提升了用户体验,...

    Asp.Net 腾讯QQ授权登录,新浪微博授权登录,OAuth2

    本教程主要聚焦于腾讯QQ和新浪微博的OAuth2授权登录实现,提供了完整的源代码,无需依赖外部DLL库。 OAuth2是一种授权框架,它允许用户将第三方应用权限授予给服务提供商,而无需分享其用户名和密码。QQ和新浪微博...

    php+QQ、微信、新浪微博登录操作类

    在PHP开发中,集成QQ、微信和新浪微博等社交媒体平台的登录功能已经成为许多网站和应用程序的标准配置,这不仅可以提升用户体验,也能帮助网站收集用户社交信息,提高用户粘性。本压缩包提供了一套PHP实现的登录操作...

    腾讯QQ,新浪,网易登录API

    本文将深入探讨腾讯QQ、新浪微博和网易的登录API,以及如何在自己的网站上实现这些登录接口。 首先,腾讯QQ的开放平台(Tencent Open API)提供了QQ账号的登录服务,允许用户通过QQ账号快速登录合作网站。QQ登录API...

    .net、asp.net使用qq登录,新浪微博,网易微博登录

    在.NET和ASP.NET开发中,实现QQ登录、新浪微博登录以及网易微博登录的功能是常见的社交网络集成需求。这些功能使得用户可以使用他们在社交媒体平台上的账户直接登录到你的网站,提供了便捷的用户体验并可能增加用户...

    QQ登录支付宝登录新浪微博快速登录ecshop插件

    QQ登录支付宝登录新浪微博快速登录ecshop插件 1、支持QQ登录, 2、新浪微博登录 3、支付宝登录 4、后台可设置管理以及支持后台管理用户分派权限 5、正常显示用户名,自动保留用户资料。 6、插件无需修改数据库...

Global site tag (gtag.js) - Google Analytics