2

Possible Duplicate:
Why is Facebook PHP SDK getUser always returning 0?

I am using the new php SDK to create a very simple app. When I try $facebook->getUser(), it always returns 0 (claiming that I'm not logged in to FB, howeven I'm already INSIDE Facebook..

It only works when I'm loggin in to the app from facebook.. any idea how to solve this?

Thanks, Yuval

Community
  • 1
  • 1
Yuvals
  • 3,094
  • 5
  • 32
  • 60
  • how do you log in to your app ? – Thomas Menga Nov 26 '11 at 11:43
  • did you request the permission to offline access to interact the application. – Punit Nov 26 '11 at 13:06
  • must I do it anyway? In all 'how-to's they don't mention I need permissions from the user. Also, I am a developer of the app.. assumed that I don't need permissions from myself.. – Yuvals Nov 26 '11 at 13:27
  • When I'm inside FB, I go to the app's page: apps.facebook.com/. The index page simply does: new Facebook(..) and $user = $facebook->getUser(); This always returns 0 – Yuvals Nov 26 '11 at 13:29
  • dont use the FB SDK. It replaces a ton of simple oauth codes so i'd recommend just using some simple scripts. – Adam Fowler Nov 26 '11 at 16:29
  • Adam, what kind of scripts? Thanks. – Yuvals Nov 26 '11 at 16:36
  • Did you ever figure this out? I'm experiencing the same problem with the SDK 3.1.1. I have an application that authenticates using FB. In my local environment I have no issues authenticating, and getting the user id, but as soon as I put it in the server (and updated the url's in my FB app page to my server url) I started getting 0 as the user id... every time! I was able to authenticate but not, the application shows as installed in my profile. I wonder if it is related to this post? https://github.com/facebook/php-sdk/issues/272 as suggested here http://stackoverflow.com/a/7607471/155248 – Onema Dec 03 '11 at 20:07

4 Answers4

1

If getUser() returns 0 than your application has not been granted permission by the user viewing.

Even if you are the developer of the application it will treat you the same, it would be hard to test much if your account behaved special.

With that said, it is very easy.

If getUser() returns 0, redirect the user to the login url. Assuming they accept you will then be able to properly use getUser().

See: https://github.com/facebook/php-sdk/blob/master/examples/example.php for a good example

savageguy
  • 1,535
  • 2
  • 13
  • 18
1

Here's what i did..

Call getUser() and if the result is 0 redirect to the login url or getLoginUrl(). Once it visit the app it will have the value for GetUser along with the signed request from Facebook.

If your first timer using the app visiting getLoginUrl will bring you to the App Permission request which you have to accept it in order to use the Facebook app.

Allan Jikamu
  • 341
  • 2
  • 8
1

Solution:

After messing with a lot of suggestions I discovered the answer is both simple and stupid. The " ' " in the following code are not true single quotation marks (or whatever they are called). Instead " ‘ " is used, which is something different thats not recognized.

Basically ‘ != '

The original code:

$config[‘appId’] = 'x0x2x1x9xx3x98x';
$config[‘secret’] = 'xxx79xf7x6xxx5xxxfxxxc5bx46xfxxx';
$config[‘fileUpload’] = false; // optional

The fixed code:

$config['appId'] = 'x0x2x1x9xx3x98x';
$config['secret'] = 'xxx79xf7x6xxx5xxxfxxxc5bx46xfxxx';
$config['fileUpload'] = false; // optional

All I did was replace the " ’ " with " ' " which caused my text editor to recognize the names as quoted strings. I uploaded it and it worked! I think it was an encoding problem somewhere along the line that changed the character, which makes sense. It means the FB team did originally write good code, but that people are having valid problems with it.

Dustin Smith
  • 61
  • 1
  • 5
0

I'm still not sure why the code in my server returns 0 every time I call $facebook->getUser(), but I found a workaround: when you create the Facebook object pass the argument 'cookie' set to true like this:

$facebook = new Facebook(array(
                'appId'  => MY_APP_ID,
                'secret' => MY_APP_SECRET,
                'cookie' => true
              ));


$userId = $facebook->getUser();

if(!empty($userId)) {
   echo "This is the user id : " . $facebook->getUser();
}

I found this information here https://github.com/facebook/php-sdk/issues/418#issuecomment-2705981

I hope this information is helpful as I have been looking for a way to make this work for a while.

Martin.
  • 10,494
  • 3
  • 42
  • 68
Onema
  • 7,331
  • 12
  • 66
  • 102