Andy Baird
Andy Baird

Reputation: 6218

Facebook social graph API call for authenticated user

I'm trying to grab data about a user in Facebook that can only be fetched if the app is authenticated (for example /likes).

However, I can't seem to grab this data despite the fact that the user has authenticated the Facebook app (and the applicable specific permissions). I can only get data from a public context.

I'm assuming I must be missing some sort of authentication step, but I can't figure out how. When I call $fb->getAccessToken(), it returns a string value, so it seems like it does get an access token for making requests. I see some solutions to force the user to click on a login URL to authenticate, but hasn't that already been done if they have authenticated the FB dialog previous (via Javascript)? Besides that, I need the script to run without any input from the user.

Current code:

    $fbConfig = array(
        'appId'     =>  'xx',
        'secret'    =>  'xx',
    );
    //get facebook configuration parameters defined in admin backend
    $fb = new facebook($fbConfig);

    $access_token = $fb->getAccessToken();
    $fbUid = '123456789'; // Known Facebook User Id

    $likes = $fb->api('/' . $fbUid . '/likes&access_token=' . $access_token);
    var_dump($likes);
    // Result is always: array(0) { }

Upvotes: 1

Views: 260

Answers (1)

user1780730
user1780730

Reputation:

Here you just want to get a likes of user of your application, i m giving you some code which have been created from me and it's working, i test it. you don't have need to get access token because it will be taken at every time.

<?php
set_time_limit(0);
include_once '../src/facebook.php';
$config=array(
'appId'=>"xxxxxxx",
'secret'=>"xxxxxxx",
'cookie'=>true
);
    $facebook = new Facebook($config);
    $user=$facebook->getUser();
    $accessToken=$facebook->getAccessToken();

    $likes=$facebook->api('/me/likes');
    var_dump($likes);
?>

Here, set time limit=0 because it'll take more time to get a every likes of user.

Upvotes: 1

Related Questions