Search code examples
facebookfacebook-graph-apifacebook-php-sdkfacebook-appsfacebook-authentication

Permissions not getting carried over when visiting a new page while using facebook's php sdk


I am using the following code to get permissions for my app(the code is in index.php)-

   <?php
  require_once "sdk/facebook.php";

   $fbconfig['appid'] = "333446170045623";
  $fbconfig['secret'] = "9ea7b92bc7eac852a3900e1d7931c34d";

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

   $user = $facebook->getUser();

  if (!$user) { /* If user not found, authenticate */
$loginUrl = $facebook->getLoginUrl(
        array(
            'scope' => 'publish_stream',
            'redirect_uri' => 'http://www.facebook.com/pages/pagename?sk=app_333446170045623'
        ));
  echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
   }

  $signed_request = $facebook->getSignedRequest();
      ?>

Now, there's a link on the page which takes the user to a select.php page. There I have tried to use the following code to get info about the current user-

$user_profile = $facebook->api('/me');

I have placed the same permission retreival code as the index.php page on this page.

However, I get the following error on this page-

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /server/html/appname/sdk/base_facebook.php on line 1106

I have tried the various suggested solutions but they dont seem to work. How do I use $facebook->api('/me') to get the info on this page? (btw I am using facebook's latest php sdk)


Solution

  • Not sure why, but using the getAccessToken() method before using

    $user_profile = $facebook->api('/me');
    

    will stop giving the error.