2

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>");
      }
    ?>
Rs Penki
  • 61
  • 2
  • 3
  • 9

3 Answers3

4

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.

mchitten
  • 392
  • 2
  • 9
  • Thank you for answers, but I get error when I use this `An error occurred. Please try again later.` when I click `Ok` It will redirect me `http://padekime.wu.lt/plaukai/index.php?error_code=1&error_msg=kError+1349040%3A+....bad program ID...` something like that... – Rs Penki Jan 26 '13 at 07:44
  • And I don't know why, but new facebook accounts doesn't get request for accept permissions, I think that is problem... – Rs Penki Jan 26 '13 at 07:50
1

I stopped using "me" keyword to get the logged in user's profile. instead of $facebook->api('/me','GET'), I changed to $facebook->api('/the facebook ID of the user','GET');

this reduce the need to do second try catch

ang
  • 679
  • 7
  • 10
0

If you do

if (!empty($user)) {}

That should help...

danny
  • 1