문제

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?

도움이 되었습니까?

해결책

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); 
}

다른 팁

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top