我想知道是否有一种方法可以使用 Facebook4J API 从 Facebook 页面获取所有(甚至最近的)帖子?
我知道可以从用户的墙或供稿中获取所有帖子,但我在 API 或文档中找不到任何内容来说明如何从页面获取帖子。
查看http://facebook4j.org/en/api-support.html#page ,看起来其实有一套Page相关的方法,但是点击其中任何一个都只是刷新页面,让我觉得它们可能是计划但尚未实现?
我知道可以使用图形 API 从页面获取帖子,但如果可能的话,我真的宁愿坚持使用 Facebook4j。
任何输入将不胜感激!
最佳答案
这是您的问题的一个最小示例: 请注意,您可以从 https://developers.facebook.com/tools/explorer 获取访问 token 和页面 ID 在您的代码中使用下面的这个 ID:
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();
}
}
}
关于java - 使用 Facebook4j api 从页面获取帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20125895/