Java 使用 Facebook4j api 从页面获取帖子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20125895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting posts from a page using Facebook4j api
提问by YYZ
I'm wondering if there's a way to use the Facebook4J API to get all (or even recent) posts from a facebook page?
我想知道是否有办法使用 Facebook4J API 从 Facebook 页面获取所有(甚至最近)帖子?
I know it's possible to get all posts from a user's wall or feed, but I can't find anything in the API or the documentation that shows how to get posts from a page.
我知道可以从用户的墙或提要中获取所有帖子,但我在 API 或文档中找不到任何显示如何从页面获取帖子的内容。
Looking at http://facebook4j.org/en/api-support.html#page, it would appear that there is in fact a set of Page related methods, but clicking on any one of them simply refreshes the page, making me think that maybe they are planned but not yet implemented?
查看http://facebook4j.org/en/api-support.html#page,看起来实际上有一组与 Page 相关的方法,但是单击其中任何一个只会刷新页面,让我想到也许它们已计划但尚未实施?
I know it's possible to get posts from a page using the graph API, but I'd really rather stick with Facebook4j, if possible.
我知道可以使用图形 API 从页面获取帖子,但如果可能的话,我真的宁愿坚持使用 Facebook4j。
Any input would be greatly appreciated!
任何投入将不胜感激!
采纳答案by roundrop
Facebook4J supports Page APIs since version 2.0.
You can get posts from a facebook page via Facebook#getFeed(PAGE_ID).
example:
Facebook4J 从 2.0 版开始支持页面 API。
您可以通过 Facebook#getFeed(PAGE_ID) 从 Facebook 页面获取帖子。例子:
ResponseList<Post> feed = facebook.getFeed("eclipse.org");
javadoc: http://facebook4j.org/javadoc/facebook4j/api/PostMethods.html#getFeed()
javadoc:http: //facebook4j.org/javadoc/facebook4j/api/PostMethods.html#getFeed()
回答by Niko Schenk
Here is a minimal example for your problem: Note that you can get the access token and the page ID from https://developers.facebook.com/tools/explorerUse this id below in your code:
这是您的问题的最小示例:请注意,您可以从https://developers.facebook.com/tools/explorer获取访问令牌和页面 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();
}
}
}