【问题标题】:Same facebook problems ... OAuthException: An active access token must be used to query information about the current user同样的facebook问题... OAuthException:必须使用活动访问令牌来查询有关当前用户的信息
【发布时间】:2013-03-14 06:32:38
【问题描述】:

所以这工作正常...但是当我刷新页面两次(或单击快速包含此脚本的两个页面)时,它会出现此错误 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。 有什么想法吗?

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

【问题讨论】:

    标签: php facebook api


    【解决方案1】:

    我个人觉得自己管理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-&gt;getUser(); 正在返回一些东西。您可能需要仔细检查它是什么。


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

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

    令人困惑。使用else

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

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2013-07-22
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 2011-11-26
      相关资源
      最近更新 更多