Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook php sdk 3.2.2 GetUser() always return 0 [duplicate]

Tags:

php

facebook

sdk

i spent all day searching for an answer.. but it seems that nothing can fix it. i guess every version had a different issue...

well it's pretty simple, i have this code:

    <?php
    include_once("facebook.php"); //include facebook SDK

    ######### edit details ##########
    $appId = '****'; //Facebook App ID
    $appSecret = '****'; // Facebook App Secret
    ##################################

    //Call Facebook API
    $facebook = new Facebook(array(
      'appId'  => $appId,
      'secret' => $appSecret,
      'cookie' => true
    ));

    $fbuser = $facebook->getUser();


    if ($fbuser) {
    // Do Something

    }
    else{               
    //Show login button for guest users
    $loginUrl = $facebook->getLoginUrl(array('redirect_uri'=>$homeurl,'scope'=>$fbPermissions));
    echo '<a href="'.$loginUrl.'"><img src="images/facebook-login.png" border="0"></a>';
    }
    ?>

i get 0 in get user every time. when i go to "login on facebook" and im not logged in, i get facebook login screen. when i go to "login on facebook" and im logged in, facebook redirect me to my page and i get 0 in get user again.

i'm tring to run it on my localhost, maybe thats the problem?

hope someone can help.. thx

like image 377
Lior Sharabi Avatar asked Mar 23 '26 22:03

Lior Sharabi


1 Answers

I had a similar problem using the same simple login script. I tried almost anything but nothing helped. (the weird thing was that the day before everything was working fine).

So started debugging the "base_facebook.php" and found that the function getAccessTokenFromCode failed without an exception (silently).

It turned out that the response of _oauthRequest gave an error message reply instead of the expected token reply:

$access_token_response =
        $this->_oauthRequest(
          $this->getUrl('graph', '/oauth/access_token'),
            $params = array ('client_id' => $this->getAppId(),
                             'client_secret' => $this->getAppSecret(),
                             'redirect_uri' => $redirect_uri,
                             'code' => $code));

So by adding echo $access_token_response; directly after this function a found my problem.

The response error was that my app was configured as a Native/Desktop app. After I changed it to Internet the getUser(); gave the correct user id.

I hope this helps.

like image 62
mewiki Avatar answered Mar 26 '26 13:03

mewiki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!