开发者

$facebook->getUser() always returns 0 [duplicate]

This question already has answers here: Closed 11 years ago.

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' => $ap开发者_StackOverflowp_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);
}


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 :).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜