Jeaf Gilbert
Jeaf Gilbert

Reputation: 12021

Retrieve Facebook Post Comments Using Graph API

I tried to get Facebook comments using:

http://graph.facebook.com/[post_id]/comments

It results only 2 of 15 comments, and without count info.

{
    "data": [
        {
            "id": "[post_id]",
            "from": {
                "name": "[name]",
                "id": "[id]"
             },
             "message": "[message]",
             "created_time": "2011-01-23T02:36:23+0000"
        },
        {
             "id": "[id]",
             "from": {
             "name": "[name]",
                 "id": "[id]"
             },
            "message": "[message]",
            "created_time": "2011-01-23T05:16:56+0000"
        }
    ]
}

Anyone know why only 2 comments?

Also, I want to retrieve comments (default number) or retrieve comments with my limit number, and get its comments count. Any idea? (Please use Graph API).

Upvotes: 43

Views: 86065

Answers (10)

Avatar
Avatar

Reputation: 15194

It results only 2 of 15 comments

Add a limit parameter to the URL:

 http://graph.facebook.com/[post_id]/comments?limit=1000&access_token=XXX

This should show all the comments.

Upvotes: 1

Keshav Gera
Keshav Gera

Reputation: 11264

After Successfully Login call this method facebookComments()

parameters.putString("fields", "message"); .............// Its Important

 AccessToken accessToken = AccessToken.getCurrentAccessToken();
    public void facebookComments() {
        try {
            getFriends(accessToken, new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                            Log.e("keshav", "one" + response);
                            CommonMethod.showAlert("res  --> " + response, MainActivity.this);
                        }
                    }
            );
        } catch (Exception e) {
            CommonMethod.showAlert("Exception is -> " + e, MainActivity.this);
        }
    }

    public void getFriends(AccessToken token, GraphRequest.Callback callback)
    {
        // TODO Comments Working but id return only
        GraphRequest requestt = new GraphRequest(token, "744511125731315_751199848395776/comments",
                null, HttpMethod.GET, callback);
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id");           // todo in use imp
        parameters.putString("fields", "name");           // todo in use imp
        parameters.putString("fields", "from");           // todo in use imp
        parameters.putString("fields", "message");           // todo in use imp
        requestt.setParameters(parameters);
        requestt.executeAsync();
    }

Upvotes: 1

Ch Ali Humayun
Ch Ali Humayun

Reputation: 41

There is a word JUGAAR in Urdu that means, finding a way out, just to get the job done. So for like purpose I made this JUGAAR, I hope it helps.

$contents = file_get_contents("http://graph.facebook.com/" . $_GET['id'] . "/likes");
if (substr_count($contents, 'name')>0) {
    echo substr_count($contents, 'name') . " people like this album";
}

By the way I am also new to this Fb stuff, I am looking for help to post comments. When I try to use graph.api./id/comments?access_token=sdfsfsdf&message="D" it still returns comments for the id instead of posting.

Upvotes: 4

Nikolay Borisov
Nikolay Borisov

Reputation: 51

I experienced the same problem with comments. The issue was that I was using an access token for a test user. Because test users don't have access to other FB users information, only the comments from pages were shown.

Upvotes: 4

mjs
mjs

Reputation: 657

SELECT comments FROM stream WHERE post_id = [yourpostid] shall not work in this case ..

the id which is returned after making a graph call successfully to post on a user's wall (using access_token of an application ) is of the form abcdef_qwerty ( underscore seperated id ) where as the post id which is mapped in the post_id of the comments table is of the form "lmnop" ..

to get the counts of like and comments on this post id of form "abcdef_qwerty" making a graph call withh app generated access token seems to be the only solution ..

something like: https://graph.facebook.com/100002619172565_117323155031656?access_token=xxxxxxxxxxxxx

Upvotes: 1

ifaour
ifaour

Reputation: 38135

You need to call it from a secure request https and provide an access_token:

https://graph.facebook.com/19292868552_118464504835613/comments?access_token=XXX

EDIT:
Added the object from the post document. try clicking the comments connection and then remove the access_token and try and see the difference.

Upvotes: 40

Enrico Susatyo
Enrico Susatyo

Reputation: 19800

As a sanity check, do you have "read_stream" permission? I can see the full comments with my access token that uses "read_stream". As mentioned by other people, you have to use https and access token as well...

Upvotes: 3

codingbadger
codingbadger

Reputation: 44042

In order to get the Like count and the comment count then you need to use a combination of the PostOwnerID and PostID not just the PostID

So for your example it would be:

https://graph.facebook.com/153125724720582_184234384932460/comments

Again, as mentioned in some of the other answers you need to use the https method along with an auth_token

Upvotes: 5

Tom Granot
Tom Granot

Reputation: 1850

You can do something like this to avoid the whole count of comments issues:

  1. Get the object's (a post is considered an object in the Graph API) ID-as I understand from your question, you already have it?
  2. Create a Comments Social Plugin with this ID, and get the code for it.
  3. Embed the code in your site.

This will result in all the comments for this object.

To get the count of comments per object, you can execute an fql query, something like this:

SELECT comments FROM stream WHERE post_id = [yourpostid]

This will return in the comments array under the count parameter the number of counts for this object.

Upvotes: 1

Pavel Surmenok
Pavel Surmenok

Reputation: 4672

Try to authenticate via App Login (http://developers.facebook.com/docs/authentication) and then to call GraphAPI with access_token prarameter.

Upvotes: 1

Related Questions