简体   繁体   中英

$facebook->getUser(); returning 0 and throwing OAuthException: An active access token must be used to query information about the current user

Before i state my problem i must say that i have visited all these SO 1 , SO 2 , SO 3 , SO 4 links and many others and none of them solving my query

with facebook-php-sdk i am trying to login a user. on first few occasions it was working perfectly fine (2 to 3 days ago.) . Today when i started again working with my project its not working any more .whenever i am trying to login a user $facebook->getUser(); throwing Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. $facebook->getUser(); throwing Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. i have also noted that getuser() is also returning 0` though few days back it was working fine.

MY CODE :: 0auth_provided_by_facebook.php

include_once $_SERVER['DOCUMENT_ROOT'].'dist/facebook-php-sdk/src/facebook.php';

$config = array(
   'appId' => '***********',
   'secret' => '*********',
   'allowSignedRequest' => false, // optional but should be set to false for non-canvas apps
'cookie'    => true,
'oauth'     => true
 );

 $facebook = new Facebook($config);
 $user_id = $facebook->getUser();
 if($user_id) {
   try {

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

  } catch(FacebookApiException $e) {
    $login_url = $facebook->getLoginUrl(array(
   scope'=>'email,user_location,user_photos,publish_actions,publish_stream,basic_info', 
    'redirect_uri'  => 'http://localhost/dist/include/0auth_provided_by_facebook.php'
    ));
    echo "EXCEPTION at place 1";
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {

  // No user, print a link for the user to login
  //$login_url = $facebook->getLoginUrl();
  $login_url = $facebook->getLoginUrl(array(
    'scope'     => 'email,user_location,user_photos,publish_actions,publish_stream,basic_info', 
    'redirect_uri'  => 'http://localhost/dist/include/0auth_provided_by_facebook.php'
    ));
  echo "Exception at place 2";

}

It is always throwing the error mentioned above and and printing Exception at place 2 . which means $user_id = $facebook->getUser();if($user_id) returnin false...


EDIT :
as mentioned in the now i have used getAccessToken and setAccessToken after $user_id = $facebook->getUser();

now my code

include_once $_SERVER['DOCUMENT_ROOT'].'dist/facebook-php-sdk/src/facebook.php';

$config = array(
'appId' => '234360450080751',
'secret' => '38189c40cec699c7c0deee2377a2c0a2',
'allowSignedRequest' => false, // optional but should be set to false for non-canvas apps
'cookie'    => true,
'oauth'     => true
 );

 $facebook = new Facebook($config);
 $user_id = $facebook->getUser();
 $access_token = $facebook->getAccessToken();
 $facebook->setAccessToken($access_token);
 //....REST ARE SAME

How to solve it these two problem ??? what is the reason ??


i am giving more details it may help you to answer my query.

my fb app settings :

  1. APP DOMAIN : kept it blank according to the comments
  2. SITE URL : http://localhost/dist/include/0auth_provided_by_facebook.php
  3. Native or desktop app? Disabled (i tried by enabling but no help)
  4. Deauthorize Callback URL : http://localhost:80/dist/
  5. Valid OAuth redirect URIs http://localhost/dist/include/0auth_provided_by_facebook.php
  6. Stream post URL security : ENABLED
  7. Normalize all publish scopes to 'publish_actions :' Enabled

I had this exact same issue with this same error being thrown. I also looked everywhere for a solution, none of which seemed to solve my particular problem.

Finally, I read Abhik Chakraborty's comment and realized that my OAuth redirect URI wasn't set within my FB App's settings. The user was authenticated but the URI didn't match when the user's info was sent back to my site, which resulted in getUser returning 0.

在设置>高级下找到它

Here's the catch: this fix didn't work while I was using localhost, even though it was setup the same as my server. I'll update my answer if I get it working with localhost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM