无法使用 facebook4j 阅读 facebook 评论
Unable to read facebook comments using facebook4j
我正在尝试使用 facebook4j library.And 阅读来自 post 的 facebook 评论,它 return 仅 post 而不是 post 的评论。它始终是 return 空的,即使在 post 中有更多的评论。谁能指导我从特定 post 获取所有评论的正确方法?
import facebook4j.Comment;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.PagableList;
import facebook4j.Post;
import facebook4j.Reading;
import facebook4j.ResponseList;
import facebook4j.auth.AccessToken;
public class PostsFromPageExtractor {
/**
* A simple Facebook4J client which
* illustrates how to access group feeds / posts / comments.
*
* @param args
* @throws FacebookException
*/
public static void main(String[] args) throws FacebookException {
// Generate facebook instance.
Facebook facebook = new FacebookFactory().getInstance();
// Use default values for oauth app id.
facebook.setOAuthAppId("", "");
// Get an access token from:
// https://developers.facebook.com/tools/explorer
// Copy and paste it below.
String accessTokenString = "PASTE_YOUR_ACCESS_TOKEN_HERE";
AccessToken at = new AccessToken(accessTokenString);
// Set access token.
facebook.setOAuthAccessToken(at);
// We're done.
// Access group feeds.
// You can get the group ID from:
// https://developers.facebook.com/tools/explorer
// Set limit to 25 feeds.
ResponseList<Post> feeds = facebook.getFeed("187446750783",new Reading().limit(25));
// For all 25 feeds...
for (int i = 0; i < feeds.size(); i++) {
// Get post.
Post post = feeds.get(i);
// Get (string) message.
String message = post.getMessage();
// Print out the message.
System.out.println(message);
// Get more stuff...
PagableList<Comment> comments = post.getComments();
String date = post.getCreatedTime().toString();
String name = post.getFrom().getName();
String id = post.getId();
}
}
}
String name = "pagename";
ResponseList<Post> feeds = facebook.getFeed(name, new Reading().limit(250));
System.out.println(feeds.size());
我认为在 2.4 版本前后,Facebook 现在要求您在发出 get 请求时指定您感兴趣的字段。如果您不指定这些字段,则只有 post 是 return。评论是这些字段之一。
要指定字段,您将使用 Reading
对象,就像您指定要 returned 的项目数一样:
// Specify all fields of interest like description, created_time, from, etc.
final Reading reading = new Reading().limit(25).fields("comments");
final ResponseList<Post> feeds = facebook.getFeed("187446750783", reading);
其余同理
我正在尝试使用 facebook4j library.And 阅读来自 post 的 facebook 评论,它 return 仅 post 而不是 post 的评论。它始终是 return 空的,即使在 post 中有更多的评论。谁能指导我从特定 post 获取所有评论的正确方法?
import facebook4j.Comment;
import facebook4j.Facebook;
import facebook4j.FacebookException;
import facebook4j.FacebookFactory;
import facebook4j.PagableList;
import facebook4j.Post;
import facebook4j.Reading;
import facebook4j.ResponseList;
import facebook4j.auth.AccessToken;
public class PostsFromPageExtractor {
/**
* A simple Facebook4J client which
* illustrates how to access group feeds / posts / comments.
*
* @param args
* @throws FacebookException
*/
public static void main(String[] args) throws FacebookException {
// Generate facebook instance.
Facebook facebook = new FacebookFactory().getInstance();
// Use default values for oauth app id.
facebook.setOAuthAppId("", "");
// Get an access token from:
// https://developers.facebook.com/tools/explorer
// Copy and paste it below.
String accessTokenString = "PASTE_YOUR_ACCESS_TOKEN_HERE";
AccessToken at = new AccessToken(accessTokenString);
// Set access token.
facebook.setOAuthAccessToken(at);
// We're done.
// Access group feeds.
// You can get the group ID from:
// https://developers.facebook.com/tools/explorer
// Set limit to 25 feeds.
ResponseList<Post> feeds = facebook.getFeed("187446750783",new Reading().limit(25));
// For all 25 feeds...
for (int i = 0; i < feeds.size(); i++) {
// Get post.
Post post = feeds.get(i);
// Get (string) message.
String message = post.getMessage();
// Print out the message.
System.out.println(message);
// Get more stuff...
PagableList<Comment> comments = post.getComments();
String date = post.getCreatedTime().toString();
String name = post.getFrom().getName();
String id = post.getId();
}
}
}
String name = "pagename";
ResponseList<Post> feeds = facebook.getFeed(name, new Reading().limit(250));
System.out.println(feeds.size());
我认为在 2.4 版本前后,Facebook 现在要求您在发出 get 请求时指定您感兴趣的字段。如果您不指定这些字段,则只有 post 是 return。评论是这些字段之一。
要指定字段,您将使用 Reading
对象,就像您指定要 returned 的项目数一样:
// Specify all fields of interest like description, created_time, from, etc.
final Reading reading = new Reading().limit(25).fields("comments");
final ResponseList<Post> feeds = facebook.getFeed("187446750783", reading);
其余同理