Facebook. Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user

access-tokenfacebookfacebook-graph-apifatal error

with my app's administrator acount on facebook my app work normally, but with other account I get error: Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user.
I had this problem before with other app (publish text on the user's wall), but fixed after i added
$user = $facebook->getUser(); What's wrong here? I have added offline_access permission… Help me, please if you can, Thank you very much.

<?php
    require_once('images/Facebook.php');

      $facebook = new Facebook(array(
        'appId'  => '456080124457246',
        'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      ));
      $user_profile = $facebook->api('/me','GET');
    $accessToken = $facebook->getAccessToken();

      # Get User ID
      $user = $facebook->getUser();
      $facebook->setAccessToken($accessToken);
      $facebook->api(456080124457246);


     if ($user) {
        try {

      # Photo Caption
      $photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';

      # Absolute Path to your image.
      $imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL

      # Post Data for Photos API
      $post_data = array(
          'message' => $photoCaption,
          'url' => $imageUrl

      );

          $apiResponse = $facebook->api('/me/photos', 'POST', $post_data);

      } catch (FacebookApiException $e) {
        error_log($e);
      }
    } else {
        $loginUrl = $facebook->getLoginUrl( array(
            'scope' => 'publish_stream,photo_upload'
        ));
        echo("<script>top.location.href = '" . $loginUrl . "';</script>");
      }
    ?>

Best Answer

In this case it's not even getting to your $facebook->getUser() call -- it's throwing an exception as soon as it reaches this line:

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

$facebook->api() is a bit of a tricky thing to work with because it throws an exception immediately if it doesn't know who "/me" is...even if you try to fix it later.

The trick, I think, is to wrap the entire thing in a try...catch block. Like so:

<?php

require_once('images/facebook.php');

$facebook = new Facebook(array(
  'appId'  => '456080124457246',
  'secret' => 'xxxxx',
));

try {
  $user_profile = $facebook->api('/me','GET');
  # Get User ID
  $user = $facebook->getUser();

 if ($user) {
    try {
  # Photo Caption
  $photoCaption = $user_profile['name'] . ' patarimų plaukams sužinojo čia http://goo.gl/otwhf';

  # Absolute Path to your image.
  $imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL

  # Post Data for Photos API
  $post_data = array(
      'message' => $photoCaption,
      'url' => $imageUrl

  );

  $apiResponse = $facebook->api('/me/photos', 'POST', $post_data);

  } catch (FacebookApiException $e) {
    error_log($e);
  }
} else {
    $loginUrl = $facebook->getLoginUrl( array(
        'scope' => 'publish_stream,photo_upload'
    ));
    echo("<script>top.location.href = '" . $loginUrl . "';</script>");
  }      

} catch (Exception $e) {
    $loginUrl = $facebook->getLoginUrl( array(
        'scope' => 'publish_stream,photo_upload'
    ));
    echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}

?>

That'll redirect the user to the login url pretty much immediately, then come back with everything in tow. You don't have to set the accessToken with this setup.

This may actually unnecessarily repeat some functionality, but hopefully it's something to start with.

By the way, for what it's worth the offline_access permission is being phased out.