1

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

Am using the following code to authenticate Facebook uses and redirecting them to login page if not logged in or require permission. Problem is getUser() always returns 0 causing the code to get stuck in a redirecting loop. Any ideas?

include_once("facebook.php");

$app_id = 'xxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';

// initialize facebook
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));

$user = $facebook->getUser();
echo $user;
if ($user) {
 try {
    $fbme = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
else
{
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email'));
header('Location: '.$loginUrl);
}
Community
  • 1
  • 1
HuwD
  • 13
  • 1
  • 1
  • 5

2 Answers2

3

After days of searching I found that I just wasn't adding a 'code' key value when using the Facebook API. The system needs it in order to validate

When you use the API function $facebook->getLoginUrl(); it takes you to a login-page that (when you authenticate properly) simply returns you to your own website with a code="x" parameter in the navigation bar. So when instantiating the Facebook Object you simply use a get request to nab this piece of information.

when your page reloads the Facebook API, make sure that one of its key values is 'code' => $_GET['code'], sorted..

Really angry that I had to figure this out myself.. Look at the getLoginUrl() docs here http://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/ and you'll find that nowhere does it specify what it returns, just that it "authorises" the app, whatever the hell that means.

Also on the main page of the PHP API docs found here http://developers.facebook.com/docs/reference/php/, it states that the minimum number of parameters required are an app secret and an appID. If this is the case, then why does adding the code sort my problem?

If I'm talking nonsense, please respond. I like to know the error of my ways :).

Mark Purnell
  • 181
  • 2
  • 10
  • This hasn't resolved it for me. Any chance you could take a look at http://stackoverflow.com/questions/9498937/why-is-my-test-application-in-an-endless-redirect-loop ? – codecowboy Feb 29 '12 at 15:27
  • @codecowboy sorry, short of copying and pasting my own code and going "do it this way" I can't help in this case :/. cut the code down to the bare minimum and find out just how minimally you can get it working. At a guess it's probably something to do with the scope parameter in your loginURL. You aren't officially logged in until you've accepted permissions, and the permissions box won't show if it's improperly configured. So this could create an endless loop, where your page tells you that you aren't logged in (perms) but the login page says that you ARE logged in. But this is just a guess. – Mark Purnell Apr 06 '12 at 12:56
  • „Really angry that I had to figure this out myself..” – that doc page explicitly says, „See the authentication page for details”. If following a link and reading some more information on another page is asking too much of you, then maybe you’d better be angry at yourself … – CBroe Jun 13 '12 at 23:05
  • 3
    The manuals have been updated since I last worked on this application. There wasn't a step by step guide to using the code to obtain an access token, and on top of that the 'code' field wasn't even mentioned. I had every right to be angry, so don't bother reviving a 3 month old thread just to throw smarm at me please. – Mark Purnell Aug 27 '12 at 11:39
1

This is not a sdk related problem, SDK (3.1) working well. getUser() and PHP-SDK silently fails if _REQUEST like globals dropping by http server if misconfigured. I was using wrong-configured nginx and after tracing code ~3 hours solved this problem via vhost configuration change.

Kev
  • 118,037
  • 53
  • 300
  • 385
edigu
  • 9,878
  • 5
  • 57
  • 80
  • 1
    What kind of wrong configuration were you using so? – cawecoy Apr 19 '13 at 12:21
  • I don't exactly remember now but i was changed nginx vhost configuration to properly pass querystring arguments to application in server {} block. I think answer is depends by configuration. – edigu Apr 19 '13 at 21:50