millebii
millebii

Reputation: 1287

app authorization with PHP sdk

I'm using the latest Facebook PHP SDK and want to simply access user data:

$facebook = new Facebook(array(  
    'appId'  => 'xxx',  
    'secret' => 'yyy',  
    'cookie' => true  
)); 
$user = $facebook->getUser();
  if ($user) { // Checks if there is already a logged in user
  try {
    // Proceed knowing you have a logged in user who's already authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
   }
}
else { 
   //Ask for bare minimum login
   $login_url = $facebook->getLoginUrl();
   header("Location: ".$login_url); 
}

My problem is that the $user is FB logged in, but $facebook->api('/me') not only fails, but I don't get the App authorization dialog either. What am I doing wrong?

Upvotes: 1

Views: 3107

Answers (2)

cloakedninjas
cloakedninjas

Reputation: 4186

Try the FB demo script at : https://github.com/facebook/facebook-php-sdk/blob/master/examples/example.php

See if that works

Upvotes: 0

millebii
millebii

Reputation: 1287

Eventually found my error so for the record:

$facebook = new Facebook(array(  
    'appId'  => 'xxxx',  
    'secret' => 'yyyyy',  
    'cookie' => true  
 )); 
$user = $facebook->getUser();
  if ($user) { // Checks if there is already a logged in user
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    // Will throw the very first time as the token is not valid
    error_log($e);
    $user = null;
   }
}
// $user is null : $user is either not logged in or the token is not valid
if(!$user) { //Ask for bare minimum login
    $login_url = $facebook->getLoginUrl();
   header("Location: ".$login_url); 
}

Upvotes: 1

Related Questions