3

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

Lior Sharabi
  • 91
  • 1
  • 9
  • Have you tried using this in conjunction with the javascript sdk? – TommyBs Mar 29 '13 at 08:45
  • As per Facebook::getUser() This method returns the Facebook User ID of the current user, or 0 if there is no logged-in user.Please use the Facebook php sdk provided in facebook developer section : https://github.com/facebook/facebook-php-sdk – Arvind Mar 29 '13 at 09:05
  • thx for the comment. my code is simple as u can see, i start to think that only Facebook could help me on this one... – Lior Sharabi Mar 29 '13 at 09:11

2 Answers2

5

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
mewiki
  • 135
  • 4
  • ye it worked, now i can see the error. i get : {"error":{"message":"Invalid redirect_uri: \u200eGiven URL is not allowed by the Application configuration.\u200e","type":"OAuthException","code":191}} maybe u know what's the problem? – Lior Sharabi Apr 04 '13 at 21:24
  • the url i gave facebook in the function and the url i gave facebook in the settings are the same... – Lior Sharabi Apr 04 '13 at 21:48
  • I spent a day trying everything to solve this, and finally I got, thanks to you. – cawecoy Apr 19 '13 at 12:59
-1

Try this, it's a piece of code a classmate of mine wrote because I was struggling with FB login aswell. Worked for me.

<?php
    session_start();
    require_once 'facebook/facebook.php';

    $facebook = new Facebook(array(
      'appId'  => '3525325', <- fake appID ofcourse, enter your own
      'secret' => 'fauuf983f9f', <- fake secret, enter your own
    ));

    $user = $facebook->getUser();

    if ($user) {
      try {
        $_fb['user'] = $facebook->api('/me');
      } catch (FacebookApiException $e) {
        error_log($e);
        $_fb['user'] = null;
      }
    }

    if ($user) 
    {
        $_fb['logouturl'] = $facebook->getLogoutUrl();
        $_fb['authed'] = true;

        echo '<h1>Logged in</h1>';
        echo 'Fbid: '.$user;

        echo "<br /><br /><pre>";
        print_r($_fb['user']);
        echo "</pre>";
    } 
    else 
    {
      $_fb['loginurl'] = $facebook->getLoginUrl(array('scope' => 'email'));
      $_fb['authed'] = false;
      echo '<a href="'.$_fb['loginurl'].'">Login with Facebook</a>';
    }
?>
<html>
    <head>
        <title>Login</title>
    </head>
    <body>
    </body>
</html>

Hope it's of use to you. Good luck :) Facebook login caused me a lot of irritation :p

Edwin Lambregts
  • 408
  • 6
  • 22