2013-05-09 75 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你是誰授權使用該應用程序的唯一的人,

相關問題