简单的faceBook第三方登录Demo

  1. 登录Facebook官网注册一个开发者账号:https://developers.facebook.com/
  2. 创建一个应用(APPS)
  3. 在自己创建的应用中可以看到自己的应用编号和应用密钥
  4. 在设置中可以创建一个回调,这个是回调地址,比如http://localhost:8080/facebookLogin
  5. 获取code值:https://www.facebook.com/dialog/oauth?client_id=应用编号&redirect_uri=回调地址&code=,这时在地址栏上会出现一长串code
  6. 获取access_token:https://graph.facebook.com/oauth/access_token?client_id=应用编号&redirect_uri=回调地址&client_secret=应用密钥&code=刚才获取到的code
  7. 获取用户资料:https://graph.facebook.com/me?access_token=刚才获取到的access_token,得到用户的id和name
  8. 开始eclipse开发,配置文件facebook.xml内容:
    <?xml version="1.0" encoding="UTF-8"?>
    <config id="facebook">
    	<params>
    		<param name="clientId" value="应用编号" />
    		<param name="secretKey" value="应用密钥" />
    		<param name="redirectUri" value="回调地址" />
    	</params>
    	<authUrl>
    	<![CDATA[
    		https://graph.facebook.com/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectUri}
    	]]>
    	</authUrl>
    	<tokenUrl>
    	<![CDATA[
    		https://graph.facebook.com/oauth/access_token?client_id=${clientId}&redirect_uri=${redirectUri}&client_secret=${secretKey}&code=${code}
    	]]>
    	</tokenUrl>
    	<userInfoApi>
    	<![CDATA[
    		https://graph.facebook.com/me?access_token=${accessToken}
    	]]>
    	</userInfoApi>
    </config>

  9. 获取配置文件信息完成登录
    @Autowired
        private IUserService userService;
    
        @RequestMapping("/getCode")
        public void getUserCode(ModelAndView model,HttpServletResponse response) 
                throws ServletException, IOException {
            String authUrl = OAuthHelper.getInfo("facebook").getAuthUrl();
            response.sendRedirect(authUrl.trim());
        }
        
        @RequestMapping("/getLogin")
        public void getUserLogin(HttpServletRequest request, HttpServletResponse response) 
                throws ServletException, IOException {
            String code = request.getParameter("code");
            String tokenURL = OAuthHelper.getInfo("facebook").getTokenUrl(code).trim();
            String ret =OAuthHelper.httpGet(tokenURL);//返回带token的json字符串
            JSONObject obj = JSON.parseObject(ret);
            String access_token=(String) obj.get("access_token");//取出token
            String userInfoApiUrl=OAuthHelper.getInfo("facebook").getUserInfoApiUrl(access_token).trim();
            String uinfoma = OAuthHelper.httpGet(userInfoApiUrl);//返回的用户信息json字符串
    
            if(uinfoma!=null){//授权成功
                JSONObject oobj = JSON.parseObject(uinfoma);
                String userid=(String) oobj.get("id");
                String thirdId="facebook_"+userid;//第三方平台的id
                User persistenceUser = userService.findUserByThirdId(thirdId);//判断本地否存在
                if(persistenceUser==null){//本地不存在则注册一个本地账号
                        User user = new User(thirdId);
                        userService.insertUser(user);
                } 
                // 用户信息存入session,跳转到用户页
                toSuccessPage(request, response);
            } else {
                // 授权过程出现错误,进入错误页
                toErrorPage(request, response);
            }
        }
    代码附件:http://download.csdn.net/download/mxy_0223/9903337
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值