简体   繁体   中英

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).

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.

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

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.

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.

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...

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.

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

尝试通过App Login(http://developers.facebook.com/docs/authentication)进行身份验证,然后使用access_token prarameter调用GraphAPI。

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM