Search code examples
javascriptfacebookfacebook-graph-apifacebook-fql

Get facebook comment by id


I have facebook comment box. I want to store the comment in a database when a user comments something. So I am attaching a callback function with FB.event.subscribe('comment.create', ... From there I get commentID and href but the only way to get the exact comment is with FQL which is deprecated from 2011 and nobody knows when facebook will remove it. Using Graph API I can get all comments but there is no way to find out which comment belongs to a specific user of our app (we don't ask for any permissions so there is no access_token; we trigger popup form when somebody comments so it is very important to match user details with comment (that's why we subscribe to comment.create)). Is there a smart way to do this or should rely on a deprecated feature?

Edit:

I am trying to get the comment like this:

FB.api(
        {
            method: 'fql.query',
            query: "SELECT text, fromid FROM comment WHERE post_fbid = '" + resp.commentID +
                "' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + resp.href + "')"
        },
        function (data) {
            var fb_id
                , comment

            console.log(data)

            if ( data.length == 1 ) {
                fb_id = data[0].fromid
                comment = data[0].text
            }

        // ...


    }
)

The problem is that when on localhost - it returns array with one element - the comment I want. When I upload to my app - then it returns array with no elements. Maybe there are permission issues. My question is how to get the content of a comment when submitted. What is the canonical way? Is it possible without access_token and permissions?


Solution

  • FQL isn't deprecated. The blog post is talking about Rest API specifically, later on it states changes with FQL.

    To access comments you need a valid access token that can view the top level object. Assuming this is just for comments on websites, a normal extended page access token should suffice by following scenario 5 explained at https://developers.facebook.com/roadmap/offline-access-removal/

    https://graph.facebook.com/oauth/access_token?             
        client_id=APP_ID&
        client_secret=APP_SECRET&
        grant_type=fb_exchange_token&
        fb_exchange_token=EXISTING_ACCESS_TOKEN 
    

    Exchange the short-lived user access token for a long-lived access token using the endpoint and steps explained earlier. By using a long-lived user access token, querying the [User ID]/accounts endpoint will now provide page access tokens that do not expire for pages that a user manages.

    Then using a page access token from [User ID]/accounts you can pretty much hard code it in (you can create your own backend login tool, in the event that you invalidate the token one day or need to change it) via a server side language for example PHP using the PHP SDK

    $facebook->setAccessToken('YOUR_PAGE_TOKEN');
    

    So from here you can do an AJAX POST to the PHP page where the SDK is loaded

    window.fbAsyncInit = function(){
    FB.Event.subscribe('comment.create',
        function(response) {
            onCommentCreate(response.commentID);
        }
    );
    
    function onCommentCreate(commentID) {
        $.ajax({
            type: 'POST',
            url: 'createcomment.php',
            data: {commentid:commentID},
            success: function(result)
            {
                alert(result);
            }
        });
    }
    
    }
    

    and request the comment information from there

    <?php
    
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset( $_POST['commentid'] ))  {
    
    $commentid = $_POST['commentid'];
    
    require 'facebook.php';
    
    $facebook = new Facebook(array(
        'appId'  => 'APP_ID_HERE',
        'secret' => 'APP_SECRET_HERE',
    ));
    
    $facebook->setAccessToken('YOUR_PAGE_TOKEN');
    
    $response = $facebook->api($commentid);
    
    echo $response['from']['id'];
    
    }
    ?>
    

    References