2013-05-09 70 views
0

美好的一天。Facebook access_token检索

我试图与脸书集成。所以我需要的用户与Facebook登录到我的网站,然后我需要得到他们的信息,如好友列表等

我做如下:

@RequestMapping("/login") 
    public void facebookLogin(HttpServletRequest request, HttpServletResponse response,String code) throws Exception 
    { 
     // ModelAndView model = new ModelAndView(); 
     // Facebook did not accept this user yet... 
     if(code==null||code.equalsIgnoreCase("")) 
     { 
      String facebook_url = "https://www.facebook.com/dialog/oauth?"; 
      //    facebook_url+="client_id="+ Settings.getStringSetting("app_id"); 
      facebook_url+="client_id="+ "12849129...344"; 
      facebook_url+="&scope=publish_actions,friends_hometown,friends_status,friends_birthday"; 
      facebook_url+="&redirect_uri="+domain+ "/facebook/login"; 
      facebook_url+="&state="+ "DDFFRREEiiFF"; 
      response.setStatus(response.SC_MOVED_TEMPORARILY); 
      response.setHeader("Location", facebook_url); 
     } 
     // Facebook accepted our user 
     else 
     { 
      // Getting token to use facebook API 
      String getTokenURL = "https://graph.facebook.com/oauth/access_token?" 
        +"client_id="+ "128491....344" 
        +"&redirect_uri="+domain+ "/facebook/login" 
        +"&code="+code 
        +"&scope=publish_actions,friends_hometown,friends_status,friends_birthday" 
        +"&client_secret="+"fc160e2ab...235254240c"; 
      //friends_hometown 
      String result = HTTPClient.sendHTTPRequestWithMethod(getTokenURL, "GET"); 
      String access_token = extractParams(result).get("access_token"); 
      // Get user... 
      String get_user_url = "https://graph.facebook.com/me?access_token="+access_token; 
      String user_str = HTTPClient.sendHTTPSRequestWithMethod(get_user_url,"GET"); 
      JSONProcessor proc = new JSONProcessor(user_str); 
      System.out.println(proc.getStructure()); 
      HashMap<String,Object> user = proc.getStructure(); 
      String facebook_id = proc.getStructure().get("id").toString(); 
      DBRecord rec = dbProc.getUserByFacebookId(facebook_id); 
      User u = new User(); 
      u.setValue("facebook_access_token",access_token); 
      u.setValue("facebook_id",facebook_id); 
      u.setValue("first_name",user.get("first_name")); 
      u.setValue("last_name",user.get("last_name")); 
      u.setValue("facebook_name",user.get("name")); 
      u.setValue("birthday",user.get("birthday")); 
      u.setValue("facebook_code",code); 
      u.setValue("facebook_link",user.get("link")); 
      u.setValue("gender",user.get("gender")); 
      u.setValue("facebook_photo_url","https://graph.facebook.com/"+user.get("id")+"/picture?type=small"); 
      if(rec==null) 
      { 

       Long user_id = u.createInDB(dbProc); 
       u.setValue("id",user_id); 
      } 
      else 
      { 
       u.setValue("id",rec.getID()); 
       u.saveToDB(dbProc); 
      } 
      setCookie(response,"facebook_code",code,86400000); 
//   return new ModelAndView("/user/main_choose"); 
      response.setStatus(response.SC_MOVED_TEMPORARILY); 
      response.setHeader("Location", "/user/main_choose"); 
     } 
    } 

所以我希望用户先来/登录没有代码参数,我重定向到https://www.facebook.com/dialog/oauth 然后,他们将登录到Facebook,它应该重定向到/登录,但添加“代码”参数,然后我可以调用https://graph.facebook.com/oauth/access_token与该“code”参数来检索access_token for当前用户。有了这个标记,我将能够做到我需要的一切。

因此,当我尝试从我的帐户(开发人员帐户)登录 - 它会很好。但是,当我尝试从其他帐户(例如我的朋友)做到这一点 - 我看到Facebook登录表单,但重定向后,我仍然没有“代码”参数。

为什么? 这里是我的应用程序配置: 网站网址:http://109.173.122.47/ 帆布网址:http://109.173.122.47/ Sabdbox模式:

+0

这不是一个回答你的问题,但希望你知道,可处理框架/简化这个给你 - 例如,我建议您查看Spring Social项目http://www.springsource.org/spring-social – GrahamMc 2013-05-09 09:13:37

回答

1

执行sandbox modeenabled

如果sandbox modeenable你是谁授权使用该应用程序的唯一的人,