adding like comment issue using graph api
Asked Answered
S

1

0

i have put last 3 posts in web page , if user loged in he can add comment / like , problem is if user loged in and try add like or comment he is getting an permission error #200 , if i loged in i can add like or comment (application is for me) , am getting the all permission nedded from the user , so how can i give him permission to add like / comment.

CODE :

$facebook = new Facebook(array(
            'appId' => '',
            'secret' => '',
            'cookie' => true,
        ));

$user = $facebook->getUser();
if ($user) {  
    if (session_id()) {

    } else {
        session_start();
    }

    $access_token = $facebook->getAccessToken();
    //check permissions list

    $permissions_list = $facebook->api(
            '/me/permissions', 'GET', array(
        'access_token' => $access_token
            )
    );

    //check if the permissions we need have been allowed by the user
    //if not then redirect them again to facebook's permissions page

    $permissions_needed = array('publish_stream', 'read_stream', 'manage_pages');

    foreach ($permissions_needed as $perm) {
        if (!isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1) {
            $login_url_params = array(
                'scope' => 'publish_stream,read_stream,manage_pages',
                 'fbconnect' =>  1,
        'display'   =>  "page",
                'redirect_uri' => 'http://localhost/fb/index.php',
            );
            $login_url = $facebook->getLoginUrl($login_url_params);
            header("Location: {$login_url}");
            exit();
        }
    }
}else {

    //if not, let's redirect to the ALLOW page so we can get access
    //Create a login URL using the Facebook library's getLoginUrl() method

    $login_url_params = array(
        'scope' => 'publish_stream,read_stream,manage_pages',
                'fbconnect' =>  1,
        'display'   =>  "page",
        'redirect_uri'=>'http://localhost/fb/index.php',
    );
    $login_url = $facebook->getLoginUrl($login_url_params);

    //redirect to the login URL on facebook
    header("Location: {$login_url}");
    exit();
}

$logoutUrl = $facebook->getLogoutUrl();

and if the user click on like button :

            jQuery('ul.fb_list a').click(function(){

               var comm_id = jQuery(this).attr("class");

                    jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
                        access_token : "<?php echo $access_token ?>"

                    });
            });  
Sneer answered 25/9, 2012 at 6:42 Comment(0)
K
0

Double check and make sure your variable comm_id is formated with both the user id of the person who posted the post, and then the id of the post itself, with an underscore in between, like this - USERID_POSTID. This is how facebook gives it you if you call to the graph api

$post_url = "https://graph.facebook.com/" . $user_id . "/posts?access_token=". urlencode($access_token);

Not sure if you're getting comm_id from another source or not. Also noticed in your code

access_token : "<?php echo $access_token ?>"

You forgot a semi-colon after the echo call. Should be this

  access_token : "<?php echo $access_token; ?>"

Hope this helps. I see you're using a lot of jquery to do the like functionality. I like to stick w/ server side code for stuff like this, I feel that it's more stable for some reason.

This is how I did it. I have a like button like this -

echo '<div id="like_container-'.$i.'">
<div id="like_count">'.$num_likes.'</div>'
<a href="javascript:void()" onclick="fb_Like(\''.$post_id.'\',\''.$access_token.'\','.$num_likes.','.$i.')"><div class="unliked"></div></a>
</div>';

and fb_Like() is an ajax call, something like this -

function fb_Like(post_id, token, num_likes, id){
$.ajax({
type: "GET",
url: "likepost.php",
data: 'id='+post_id+'&token='+token+'&likes='+num_likes,
success: function(html)
{
    $("#like_container-"+id).empty().html(html);

}

});
}

And the likepost.php page is a script similar to the one on this page

Like a Facebook Post externally using Graph Api - example

This worked really well for me. It also let's me update the number of likes that the post has on the front end right above the like button if a like has been made. Good luck!

UPDATE

If you want to check if the user already likes a post, it's pretty simple w/ the facebook graph api

//Create Post Url
$post_url = "https://graph.facebook.com/" . $Page/User_id . "/posts?access_token=". urlencode($access_token);

//Get Json Contents
$resp = file_get_contents($post_url,0,null,null);

//Store Post Entries as Array
$the_posts = json_decode($resp, true);

foreach ($the_posts['data'] as $postdata) {

    foreach ($postdata['likes']['data'] as $like){

        if($like['id']==$user){

        $liked=1;

        }else{continue;}                                                
    }

    if($liked==1){
     //do something
    }

}

This assumes that you already have a facebook user id for the logged in user, in this example, stored in the variable $user.

Keynote answered 26/9, 2012 at 3:25 Comment(9)
what is num_likes , id ... can you add some comment pleaseSneer
what if the user already made an like for that post so , you need to check for each post you are getting if that user already checked that post or not , if not show LIKE ,else UNLIKE .Sneer
For your first comment, num_likes would be the number of likes the post already has, so u can update it if a new like. id would be for if u has a list of posts that u want ur user to like..if u give them each a unique id u can have 1 function that will process the like and then update the proper post w/ the new like countKeynote
$Page/User_id above is the id of the facebook page or user that the posts are coming fromKeynote
can i add like/comment to an post inside facebook page ?? i have trird but am not able to do , and am not getting error or anything !! have any idea ?Sneer
I'm not sure I understand your question. With the method above it should post the like to the facebook post, and you should see that like within facebook if you go to the post. Is that what you mean? If you're talking about liking/commenting a post from a page and not a person, just use the page id in place of the user id.Keynote
yes am talking about liking/commenting a post from a page not a person , am sending the like = graph.facebook.com". $post_id."/like?access_token=tocken , but am getting nothing ... do i missed something !!Sneer
if you open any facebook page , and you are not liked that page , for example , you add like for any post in that page the post will be liked , in the same time the page it self will be liked as well ,, so am not sure if i can like post in page ,,, did you tried do that before ?!!Sneer
#12659790Sneer

© 2022 - 2024 — McMap. All rights reserved.