Java: 使用 RestFB 获取 facebook 群组列表
Java: get list of group on facebook using RestFB
我正在编写一个应用程序来获取用户当前加入的所有组数据。但我不知道为什么它只适用于我的 facebook 帐户(也是应用程序的所有者),对于其他帐户,列表 return null.
这是我用来创建 url 供用户登录的代码
AppInfo appInfo = new AppInfo();
String appId = appInfo.getAppId();
String redirectUrl = URLEncoder.encode(appInfo.getRedirectUrl(), "utf-8");
List<String> list = appInfo.getPermission();
StringBuilder builder = new StringBuilder();
for(String permission : list){
if(builder.length() > 0)
builder.append(",");
builder.append(permission);
}
String url = "https://graph.facebook.com/oauth/authorize?display=popup&client_id="+appId+
"&redirect_uri="+redirectUrl+"&scope="+builder.toString();
登录成功时的操作(获取访问令牌):
@RequestMapping(value="/success")
public ModelAndView success(@RequestParam(value = "code") String authorizationCode) throws IOException{
AppInfo appInfo = new AppInfo();
String appId = appInfo.getAppId();
String secretKey = appInfo.getAppSecret();
String redirectUrl = URLEncoder.encode(appInfo.getRedirectUrl(), "utf-8");
WebRequestor wr = new DefaultWebRequestor();
WebRequestor.Response accessTokenResponse = wr.executeGet(
"https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&redirect_uri=" + redirectUrl
+ "&client_secret=" + secretKey + "&code=" + authorizationCode);
FacebookClient.AccessToken token = DefaultFacebookClient.AccessToken.fromQueryString(accessTokenResponse.getBody());
String accessToken = token.getAccessToken();
ModelAndView mav = new ModelAndView();
mav.setViewName("success");
mav.addObject("authorizationCode", authorizationCode);
mav.addObject("accessToken", accessToken);
return mav;
}
最后一个是显示组列表的操作:
@RequestMapping(value="/facebookData", method=RequestMethod.POST)
public String autoFacebook(@RequestParam("accessToken") String accessToken, Model model){
FacebookClient fbClient;
Post firstGroupPost = null;
fbClient = new DefaultFacebookClient(accessToken, Version.VERSION_2_3);
Connection<Group> group = fbClient.fetchConnection("me/groups", Group.class);
if(!group.getData().isEmpty()){
Group firstGroup = group.getData().get(0);
Connection<Post> groupPost = fbClient.fetchConnection(firstGroup.getId()+"/feed", Post.class);
firstGroupPost = groupPost.getData().get(0);
}
model.addAttribute("firstGroupPost", firstGroupPost);
model.addAttribute("group", group);
return "facebook";
}
user_group
权限是扩展权限。因此,要与相应应用的 admin/tester/developer 以外的其他用户一起使用,该应用需要接受登录审核。
见
但是你会遇到问题:
This permission is granted to apps building a Facebook-branded client on platforms where Facebook is not already available. For example, Android and iOS apps will not be approved for this permission. In addition, Web, Desktop, in-car and TV apps will not be granted this permission.
我正在编写一个应用程序来获取用户当前加入的所有组数据。但我不知道为什么它只适用于我的 facebook 帐户(也是应用程序的所有者),对于其他帐户,列表 return null.
这是我用来创建 url 供用户登录的代码
AppInfo appInfo = new AppInfo();
String appId = appInfo.getAppId();
String redirectUrl = URLEncoder.encode(appInfo.getRedirectUrl(), "utf-8");
List<String> list = appInfo.getPermission();
StringBuilder builder = new StringBuilder();
for(String permission : list){
if(builder.length() > 0)
builder.append(",");
builder.append(permission);
}
String url = "https://graph.facebook.com/oauth/authorize?display=popup&client_id="+appId+
"&redirect_uri="+redirectUrl+"&scope="+builder.toString();
登录成功时的操作(获取访问令牌):
@RequestMapping(value="/success")
public ModelAndView success(@RequestParam(value = "code") String authorizationCode) throws IOException{
AppInfo appInfo = new AppInfo();
String appId = appInfo.getAppId();
String secretKey = appInfo.getAppSecret();
String redirectUrl = URLEncoder.encode(appInfo.getRedirectUrl(), "utf-8");
WebRequestor wr = new DefaultWebRequestor();
WebRequestor.Response accessTokenResponse = wr.executeGet(
"https://graph.facebook.com/oauth/access_token?client_id=" + appId + "&redirect_uri=" + redirectUrl
+ "&client_secret=" + secretKey + "&code=" + authorizationCode);
FacebookClient.AccessToken token = DefaultFacebookClient.AccessToken.fromQueryString(accessTokenResponse.getBody());
String accessToken = token.getAccessToken();
ModelAndView mav = new ModelAndView();
mav.setViewName("success");
mav.addObject("authorizationCode", authorizationCode);
mav.addObject("accessToken", accessToken);
return mav;
}
最后一个是显示组列表的操作:
@RequestMapping(value="/facebookData", method=RequestMethod.POST)
public String autoFacebook(@RequestParam("accessToken") String accessToken, Model model){
FacebookClient fbClient;
Post firstGroupPost = null;
fbClient = new DefaultFacebookClient(accessToken, Version.VERSION_2_3);
Connection<Group> group = fbClient.fetchConnection("me/groups", Group.class);
if(!group.getData().isEmpty()){
Group firstGroup = group.getData().get(0);
Connection<Post> groupPost = fbClient.fetchConnection(firstGroup.getId()+"/feed", Post.class);
firstGroupPost = groupPost.getData().get(0);
}
model.addAttribute("firstGroupPost", firstGroupPost);
model.addAttribute("group", group);
return "facebook";
}
user_group
权限是扩展权限。因此,要与相应应用的 admin/tester/developer 以外的其他用户一起使用,该应用需要接受登录审核。
见
但是你会遇到问题:
This permission is granted to apps building a Facebook-branded client on platforms where Facebook is not already available. For example, Android and iOS apps will not be approved for this permission. In addition, Web, Desktop, in-car and TV apps will not be granted this permission.