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

本小妞迷上赌 提交于 2019-12-09 19:34:52

问题


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>");
      }
    ?>

回答1:


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.




回答2:


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




回答3:


If you do

if (!empty($user)) {}

That should help...



来源:https://stackoverflow.com/questions/14525731/facebook-fatal-error-uncaught-oauthexception-an-active-access-token-must-be-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!