1

Before i say something i want to say that i GOOGLEd a lot and there were lots of pages found(including many of so's question) with varieties of solutions ... but i have tried each of them one by one but unfortunately none of them worked for me

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() is returning 0.Buts 3 days back it was working.

i have used the code given here :: developers.facebook.com and yes i have made some changes those are given with the full code below

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 '<a href="' . $login_url .'"> PLEASE CLICK HERE </a>';
    error_log($e->getType());
    error_log($e->getMessage());
  }   
} else {

  $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 '<a href="' . $login_url .'"> PLEASE CLICK HERE </a>';

}

OUTPUT : getUser() is always returning 0.


how my login process works described below :

after clicking on <a href="login_fb">Login using fb </a> control goes to my localhost/dist/index.php now my index.php include the above wriiten code which is in different directory localhost/dist/include/0auth_provided_by_facebook.php from this file i handle everything. on successful and failure of login facebook returns value here.and then on successful login user is again redirected to home


Now here is he full list of my app setting which i enabled or disabled or changed

 1. App Domains : blank(according to the answer)
 2. Site URL : `http://localhost/dist/include/0auth_provided_by_facebook.php?` 
 3. Native or desktop app? Disabled
 4. Deauthorize Callback URL : `http://localhost/dist/include/0auth_provided_by_facebook.php?`
 5. Social Discovery : enabled
 6. Client OAuth Login : enabled
 7. Embedded browser OAuth Login :enabled
 8. App Secret Proof for Server API calls :enabled
 9. Require 2-factor reauthorization : disabled
10. Valid OAuth redirect URIs :`http://localhost/dist/include/0auth_provided_by_facebook.php?` (according to the suggesion given in the answer)
11. Stream post URL security : enabled

these were the details. $user_id = $facebook-> getUser(); is always returning 0 in all condition. how can i solve it?


for more info .. thhough i dont know it will help you not...i was going through my base_facebook.php and i found that it is returning zero because of if(signed_request)is always false.
RbG
  • 3,181
  • 3
  • 32
  • 43
  • Why do you have the deauth callback same target with the site? – Jimmy Kane Feb 23 '14 at 15:43
  • Does the user authorize your app? Does the flow happen and still you get user id 0 ? Is that correct? Is that your problem ? – Jimmy Kane Feb 23 '14 at 15:44
  • yes the user authorize my app ... it is mentioned in roles in developers.facebook.com .... and >still you get user id 0 ? Is that correct? yes thats my problem – RbG Feb 23 '14 at 15:48

4 Answers4

0

To get rid of this issue you need to do the following

Change

$login_url = $facebook->getLoginUrl(array(
    'scope'     => 'email,user_location,user_photos,publish_actions,basic_info', 
    'redirect_uri'  => 'http://localhost/dist/'
    ));

to

$login_url = $facebook->getLoginUrl(array(
    'scope'     => 'email,user_location,user_photos,publish_actions,basic_info', 
    'redirect_uri'  => 'http://localhost/dist/index.php'
    ));

Leave domain name blank in the following setting. Dont give localhost.com

App Domains : localhost.com

Then

Valid OAuth redirect URIs :http://localhost/dist/

to

Valid OAuth redirect URIs :`http://localhost/dist/index.php

Now try there after.

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
0

change your app secret from app configuration page.then use this new app secret in above page you have mentioned.Delete all the cookies in browser and try loading the page you will get the correct response.

0

to me it sounds issue of local host if you can use a domain to test your code i have a one page login system running on my website i can send you all code how ever you code is looking good.

m-farhan
  • 1,436
  • 13
  • 14
0

Edit base_facebook.php library and replace existing getCode() function with

protected function getCode() {
$server_info = array_merge($_GET, $_POST, $_COOKIE);
    if (isset($server_info['code'])) {
        if ($this->state !== null && isset($server_info['state']) && $this->state === $server_info['state']) {
            $this->state = null;
            $this->clearPersistentData('state');
            return $server_info['code'];
        } else {
            self::errorLog('CSRF state token does not match one provided.');
            return false;
        }
    }
    return false;
}
Wasim A.
  • 9,660
  • 22
  • 90
  • 120