Unable to read facebook comments using facebook4j

I am trying to read facebook comments from a post using facebook4j library.And it returns only post but not comments for that post. It is always return empty even there are more comments in any post. Could anyone guide me the correct way to get all comments from a particular 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());

Upvotes: 2

Views: 1111

Answers (1)

kilemensi
kilemensi

Reputation: 58

I think around version 2.4, Facebook now requires you to specify the fields you are interested in when you issue a get request. If you don't specify these fields, only the post is return. Comments are one of those fields.

To speficy the fields you'd use the Reading object just like you would in specifying the number of items to be 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);

The rest should be the same.

Upvotes: 3

Related Questions