再会。
我正在尝试与 facebook 集成。所以我需要用户使用 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");
}
}
所以我希望用户首先来到 /login 而没有 code 参数,然后我将他们重定向到https://www.facebook.com/dialog/oauth
然后他们将登录到 facebook,它应该将他们重定向回 /login,但添加“code”参数,然后我可以https://graph.facebook.com/oauth/access_token
用那个“code”调用" 用于检索当前用户的 access_token 的参数。有了这个令牌,我就可以做我需要的一切。
因此,当我尝试从我的帐户(开发人员帐户)登录时-一切正常。但是当我尝试从其他帐户(例如我朋友的帐户)执行此操作时 - 我看到了 facebook 登录表单,但重定向后我仍然没有“代码”参数。
为什么?这是我的应用程序配置:站点 URL:http://109.173.122.47/
画布 URL:http://109.173.122.47/
Sabdbox 模式:ON