使用PHP sdk进行应用授权

时间:2011-09-05 07:44:09

标签: php facebook-php-sdk

我正在使用latest Facebook PHP SDK并希望只访问用户数据:

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

我的问题是$user已登录FB,但$facebook->api('/me')不仅失败,而且我也没有获得应用授权对话框。 我究竟做错了什么?

2 个答案:

答案 0 :(得分:1)

最终发现我的错误是为了记录:

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

答案 1 :(得分:0)