同样的facebook问题…oautheexception:必须使用活动访问令牌来查询当前用户的信息


Same facebook problems ... OAuthException: An active access token must be used to query information about the current user

所以这是正常工作…但是,当我刷新页面两次(或点击两个页面谁包含这个脚本快速),它给出了这个错误oautheexception:必须使用活动访问令牌来查询当前用户的信息。什么好主意吗?

<?php
  $app_id = '***************';
  $app_secret = '**************';
  $app_namespace = '****************';
  $app_url = 'http://apps.facebook.com/' . $app_namespace . '/';
  $scope = 'email,publish_actions';
  // Init the Facebook SDK
   $facebook = new Facebook(array(
     'appId'  => $app_id,
     'secret' => $app_secret,
   ));
   // Get the current user
   $user = $facebook->getUser();
   // If the user has not installed the app, redirect them to the Login Dialog
   if (!$user) {
     $loginUrl = $facebook->getLoginUrl(array(
       'scope' => $scope,
       'redirect_uri' => $app_url,
     ));
     print('<script> top.location.href=''' . $loginUrl . '''</script>');
   }
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me', 'POST');
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
//<img src="https://graph.facebook.com/?php echo $user; ?/picture">
//?php print_r($user_profile); ?
?>

我个人觉得自己管理access_token更容易。Facebook的Graph API通过要求传递access_token来保护受保护的端点。PHP SDK将这些抽象出来,但我从来没有对Facebook处理这些信息感到满意,因为有时它就是不起作用。这并不是说这个库不好,只是说我没有正确地使用。请记住这个警告。

看起来你正在使用Facebook Canvas应用程序。当用户第一次成功验证时,Facebook将在$_GET中发送access_token。此时,您应该将其保存到数据库中,因为该访问令牌的有效期为3个月左右。

此时,从那时起,可以在调用参数中传入access_token:

try {
    $user_profile = $facebook->api('/me', 'POST', array(
        "access_token" => ''    // access token goes here
    ));
} catch (FacebookApiException $e) {
    // error handling
}

考虑到Facebook正在返回您需要访问令牌才能调用/me资源的错误,看起来$facebook->getUser();正在返回一些东西。你可能需要再检查一下它是什么。


当我在这里的时候,你在使用这个逻辑:

if (!conditional) {
    // do something
}
if (conditional) {
    // do something else
}

让人困惑。使用else:

if (conditional) {
    // do something
} else {
    // do something else
}
相关文章: