Receive messages from a page using Facebook4j api

I am wondering if there is a way to use the Facebook4J API to receive all (or even recent) posts on a facebook page?

I know that you can get all messages from a user’s wall or feed, but I can’t find anything in the API or documentation that shows how to receive messages from a page.

Looking at http://facebook4j.org/en/api-support.html#page , it seems that there really is a set of page-related methods, but clicking on any of them just refreshes the page, making me think that maybe are they planned but not yet implemented?

I know that you can receive messages from a page using the graphical API, but I would prefer to stick with Facebook4j if possible.

Any entry is welcome!

+6
source share
2 answers

Facebook4J supports page APIs from version 2.0.
You can receive messages on your facebook page via Facebook # getFeed (PAGE_ID). Example:

ResponseList<Post> feed = facebook.getFeed("eclipse.org"); 

javadoc: http://facebook4j.org/javadoc/facebook4j/api/PostMethods.html#getFeed ()

+10
source

Here is a minimal example of your problem: Please note that you can get the access token and page ID from https://developers.facebook.com/tools/explorer Use this ID below in your code:

 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(); } } } 
+11
source

Source: https://habr.com/ru/post/958577/


All Articles