开放平台-web实现人人网第三方登录
应用场景
操作步骤
登录流程


案例实战
本地开发环境准备
前端登录跳转页面
<html>
<head>
<title>人人网登录跳转</title>
<script src="http://lib.sinaapp.com/js/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> <script type="text/javascript">
//应用的APIKEY
var apiKey = "3ce9cb1e264f4e93b1f38807be66e629";
//成功授权后的回调地址
var redirectUrl = "@@{openapi.Renrens.callback()}"; var authorizeUrl = "https://graph.renren.com/oauth/authorize?"
var queryParams = ['client_id=' + apiKey,'redirect_uri=' + redirectUrl,'response_type=code'];
var url = authorizeUrl + queryParams.join('&'); //打开授权登录页面
window.location.href= url;
</script>
</head> <body>
</body>
</html>
server端处理回调,同步信息
/**
* 授权回调
*
* @param code
* @param error
*/
public static void callback(String code) {
if (!StringUtils.isEmpty(code)) {
error("授权失败");
} // 根据code换取accesstoken,包括用户信息
// ... String callbackUrl = RouteContext.getUrl("openapi.Renrens.callback", Collections.EMPTY_MAP,
true);
RenrenToken token = RenApi.getTokenInfo(code, callbackUrl);
if (token == null) {
error("授权失败:无法获取连接系统");
}
render(code, token);
}
/**
* 返回token数据对象
*
* <pre>
* * {
* "token_type":"bearer",
* "expires_in":2595096,
* "refresh_token":"127021|0.KAS3b8doSitHk6RLDtitb2VY8PjktTRA.229819774.1376381303243",
* "user":{
* "id":229819700,
* "name":"二小姐",
* "avatar":[
* { "type":"avatar",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_head_KFTQ_d536000000d0111b.jpg"
* },
* { "type":"tiny",
* "url":"http://hdn.xnimg.cn/photos/hdn221/20130805/2055/tiny_jYQe_ec4300051e7a113f.jpg"
* },
* { "type":"main",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_main_ksPJ_d536000000d0111b.jpg"},
* { "type":"large",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_large_yxZz_d536000000d0111b.jpg"
* }
* ]
* },
* "access_token":"127066|6.08718aa138db0578dda3250f33bads6e.2592000.1378976400-229819774"
* "scope":"read_user_feed read_user_album",
* </pre>
*
* @author littleatp
* @createDate 2015年4月14日
*
*/
public class RenrenToken { public String token_type;
public int expires_in;
public String refresh_token;
public String access_token;
public String scope; public RenrenUser user;
}
/**
* 人人网用户信息
*
* <pre>
* "user":{
* "id":229819700,
* "name":"二小姐",
* "avatar":[
* { "type":"avatar",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_head_KFTQ_d536000000d0111b.jpg"
* },
* { "type":"tiny",
* "url":"http://hdn.xnimg.cn/photos/hdn221/20130805/2055/tiny_jYQe_ec4300051e7a113f.jpg"
* },
* { "type":"main",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_main_ksPJ_d536000000d0111b.jpg"},
* { "type":"large",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_large_yxZz_d536000000d0111b.jpg"
* }
* ]
* },
* </pre>
*
* @author littleatp
* @createDate 2015年4月14日
*
*/
public class RenrenUser { public long id;
public String name;
public List<RenrenAvatar> avatar; public String getAvatarUrl() {
if (avatar == null || avatar.isEmpty()) {
return "";
}
return avatar.get(0).url;
} public static class RenrenAvatar {
public String type;
public String url;
}
}
/**
* 人人网API
*
* <pre>
* 登录流程:
*
* 1 前端跳转人人网授权(code方式)
* 2 回调获得authorize code
* 3 通过code换取access_token
* 4 获得token及用户信息
*
* 参考文档:
* http://wiki.dev.renren.com/wiki/Authentication
* </pre>
*
*
* @author littleatp
* @createDate 2015年4月10日
*
*/
public class RenApi { public static String apiKey = "xxx";
public static String secretKey = "xxx"; public static String baseUrl = "https://graph.renren.com/oauth"; protected static final String URL_GET_TOKEN = baseUrl + "/token?grant_type=authorization_code"
+ "&client_id=%s&client_secret=%s&code=%s&redirect_uri=%s"; protected static final long ACCESS_TIMEOUT = 15; protected static final String DEF_APP_TOKEN_EXPIRE = "3h";/**
* 获取token信息
*
* <pre>
* http://wiki.dev.renren.com/wiki/Authentication#.E5.AE.A2.E6.88.B7.E7.AB.AF.E6.8E.88.E6.9D.83
* 返回token的同时也附带了用户信息
*
* 调用地址:
* https://graph.renren.com/oauth/token
*
* 参数
* grant_type:使用Authorization Code 作为Access Grant时,此值固定为“authorization_code”;
* client_id:在开发者中心注册应用时获得的API Key;
* client_secret:在开发者中心注册应用时获得的Secret Key。Secret Key是应用的保密信息,请不要将其嵌入到服务端以外的代码里;
* redirect_uri:必须与获取Authorization Code时传递的“redirect_uri”保持一致;
* code:上述过程中获得的Authorization Code。
*
* 返回结果如下:
* {
* "token_type":"bearer",
* "expires_in":2595096,
* "refresh_token":"127021|0.KAS3b8doSitHk6RLDtitb2VY8PjktTRA.229819774.1376381303243",
* "user":{
* "id":229819700,
* "name":"二小姐",
* "avatar":[
* { "type":"avatar",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_head_KFTQ_d536000000d0111b.jpg"
* },
* { "type":"tiny",
* "url":"http://hdn.xnimg.cn/photos/hdn221/20130805/2055/tiny_jYQe_ec4300051e7a113f.jpg"
* },
* { "type":"main",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_main_ksPJ_d536000000d0111b.jpg"},
* { "type":"large",
* "url":"http://hdn.xnimg.cn/photos/hdn121/20130805/2055/h_large_yxZz_d536000000d0111b.jpg"
* }
* ]
* },
* "access_token":"127066|6.08718aa138db0578dda3250f33bads6e.2592000.1378976400-229819774"
* "scope":"read_user_feed read_user_album",
* }
*
* 错误返回:
* {
* "error": "invalid_grant",
* "error_code": 20204
* "error_description": "Invalid authorization code: 9OCQp3IzRcwtSRPKOEUKiRRsz9SUNgdE"
* }
* http://wiki.dev.renren.com/wiki/%E9%94%99%E8%AF%AF%E5%93%8D%E5%BA%94
* </pre>
*
* @param accessToken
* @return
*/
public static RenrenToken getTokenInfo(String code, String callbackUrl) {
if (StringUtils.isEmpty(code)) {
return null;
} String url = String.format(URL_GET_TOKEN, apiKey, secretKey, code, callbackUrl); String resultString = DefaultHttp.get(url, ACCESS_TIMEOUT, GlobalConstants.UTF_8); Logger.debug("[sso-renren]get token. use url '%s'", url); RenrenToken token = JsonUtil.fromJson(resultString, RenrenToken.class);
if (token == null || StringUtils.isEmpty(token.access_token)) {
Logger.debug("[sso-renren]get token failed, with result of '%s'", resultString);
return null;
} Logger.debug("[sso-renren]get token success, with result of '%s'", resultString);
return token;
}
}
关于CSRF
常见问题
开放平台-web实现人人网第三方登录的更多相关文章
- 开放平台-web实现QQ第三方登录
应用场景 web应用通过QQ登录授权实现第三方登录. 操作步骤 1 注册成为QQ互联平台开发者,http://connect.qq.com/ 2 准备一个可访问的域名, ...
- web实现QQ第三方登录 开放平台-web实现QQ第三方登录
应用场景 web应用通过QQ登录授权实现第三方登录. 操作步骤 1 注册成为QQ互联平台开发者,http://connect.qq.com/ 2 准备一个可访问的域名, ...
- web实现QQ第三方登录
开放平台-web实现QQ第三方登录 应用场景 web应用通过QQ登录授权实现第三方登录. 操作步骤 1 注册成为QQ互联平台开发者,http://connect.qq.com ...
- 微信开放平台PC端扫码登录功能个人总结
最近公司给我安排一个微信登录的功能,需求是这样的: 1.登录授权 点击二维码图标后,登录界面切换为如下样式(二维码),微信扫描二维码并授权,即可成功登录: 若当前账号未绑定微信账号,扫描后提示“ ...
- 腾讯开放平台web第三方登录获取信息类(包含签名)
不清楚具体参数的可以先看下第三方登录的文档: class QQ { //$appid 你的appid //$openid 获取到的唯一的用户openid //$openkey 获取到的openkey ...
- iOS5.0以上使用新浪微博开放平台OAuth 续(及解决登录无效问题)
新浪微博开放平台为第三方应用提供了简便的合作模式,满足了手机用户和平板电脑用户随时随地分享信息的需求.通过调用平台的api即可实现很多微博上的功能. 本篇主要目的是记录新浪微博移动SDK iOS版本的 ...
- 微信开放平台,微信登陆第三方网站 提示redirect_uri 参数错误
在微信开放平台上我填写的回调域是:bbs.qiaoshisui.com/LoginApi/WeiXinCallBack,我构造的链接是:https://open.weixin.qq.com/conne ...
- ASP.NET Web API与Owin OAuth:调用与用户相关的Web API(非第三方登录)
授权完成添加属性 ClaimsIdentity oAuthIdentity = await CreateAsync(user/*userManager*/, OAuthDefaults.Authent ...
- 微信开放平台--》网站应用开发 微信登录网站接口(https://open.weixin.qq.com/)
地址:https://open.weixin.qq.com/ 手册:https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&am ...
随机推荐
- selectors实现高并发
1. 下面的例子,客户端给服务端发送消息,服务端把消息返回 server #!/usr/bin/env python import selectors import socket import tim ...
- HDU 4349 Xiao Ming's Hope
有这样一个性质:C(n,m)%p=C(p1,q1)*C(p2,q2).......%p,其中pkpk-1...p1,qkqk-1...q1分别是n,m在p进制下的组成. 就完了. #include&l ...
- yii2归档安装
1.http://www.yiiframework.com/download/ 下载文件 2.如果inint.bat文件一闪而过,没有提示是开发还是生产环境 用编辑器(phpstorm)打开文件在对应 ...
- cocos2dx 搭建 android 平台 -2
1.配置Cocos2d-x for Xcode. 这一块比较简单, 可以参见其他文章. 重点:install-templates-xcode.sh 2.配置普通Android开发环境 这一块包括JDK ...
- 高级iOS开发工程师的面试题
1:CALayer与UIView的区别是什么? 两者最大的区别就是:涂层不会直接渲染到屏幕上: UIView是iOS界面元素的基础,所有界面元素都是继承于它,他的本身全是由CoreAnimation来 ...
- 转:Bat命令学习
转:http://www.cnblogs.com/SunShineYPH/archive/2011/12/13/2285570.html一.基础语法: 1.批处理文件是一个“.bat”结尾的文本文件, ...
- nginx+lua项目学习
1.启动nginx命令 /usr/local/ngx_openresty/nginx/sbin/nginx -p /usr/local/ngx_openresty/nginx/ -c /usr/loc ...
- LeetCode Binary Tree Right Side View (DFS/BFS)
题意: 给一棵二叉树,要求收集每层的最后一个节点的值.按从顶到底装进vector返回. 思路: BFS比较简单,先遍历右孩子就行了. /** * Definition for a binary tre ...
- RESTful 杂文归集
Json格式与对象转换 org.codehaus.jackson.map.ObjectMapper 工具转json格式,进行接口文档书写工具http://editor.swagger.io/#/ ja ...
- 机器学习技法-GBDT算法
课程地址:https://class.coursera.org/ntumltwo-002/lecture 之前看过别人的竞赛视频,知道GBDT这个算法应用十分广泛.林在第八讲,简单的介绍了AdaBoo ...