0

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. 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
Community
  • 1
  • 1
RbG
  • 3,181
  • 3
  • 32
  • 43
  • what is the domain name specified in the setting page for your app in facebook ? if its something as localhost.. then remove it and save the settings and try again. – Abhik Chakraborty Mar 09 '14 at 09:33
  • yes it is localhost... if i need to replace it as you said i will replace it with what?? – RbG Mar 09 '14 at 09:34
  • i had **`localhost.com`** as app domain ... removed it but still not working – RbG Mar 09 '14 at 09:37
  • 1
    hmm so login works but after it comes back to the page it shows error ? make sure that if you have some redirect url specified on app setting you should use the same in redirect_uri in the login param. – Abhik Chakraborty Mar 09 '14 at 09:43
  • @AbhikChakraborty those are all same .. there must be some other problem i think – RbG Mar 09 '14 at 09:46
  • your login code is in the same page as u mentioned? or its a different page and u are re-directing to the above page ? – Abhik Chakraborty Mar 09 '14 at 09:48
  • @AbhikChakraborty ... after user clicks on `login using facebook` my index.php redirects the control to this page after performing success full login facebook returns to this page and from here i exit() and go to home page ... that is the functionality – RbG Mar 09 '14 at 09:51
  • ah i see so the issue is not getUser() its the $facebook->api('/me','GET'); which is throwing the error. You can fix it by - use iduser in instead of /me as $facebook->api('/$user_id ','GET'); - OR use $access_token = $facebook->getAccessToken(); $facebook->setAccessToken($access_token); and then call api('/me').. this should fix it. – Abhik Chakraborty Mar 09 '14 at 09:55
  • @AbhikChakraborty .... :( :( ...sorry none solved it...is there really any way???..what is the problem..tring to solve it for more than 4 hours..no result – – RbG Mar 09 '14 at 11:25
  • same problem here, I've been struggling with Facebook oAuth via PHP SDK since yesterday. No way to get the SDK getUser() to answer anything else than 0. All suggestions are welcomed. – Didier Sampaolo Mar 09 '14 at 15:30
  • There are few reasons which could lead to return iduser as 0 and they are - $_REQUEST method is not working and the lib is not able to get the data - some sort of settings which is not working well with the app - cookie is not set up properly using nested folders – Abhik Chakraborty Mar 09 '14 at 15:39

1 Answers1

0

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.

Find this under Settings > Advanced

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.

Jerad
  • 594
  • 7
  • 15