PHP SDK v.3.1.1 - 在简单示例中,“必须使用活动访问令牌”

时间:2011-10-05 17:10:18

标签: facebook sdk facebook-php-sdk

我有一些实施最新PHP SDK和OAuth2的大错误,因此我将其分解为使用提供的示例来演示我遇到的问题。

我正在使用此代码。我收到OAuthException'必须使用活动访问令牌来查询有关当前用户的信息。'当我点击登录Facebook链接时,我继续收到此错误,我没有连接。请注意,这通常在所有cookie和会话都已清除时起作用,但是如果我返回它几个小时后就不再有效了。

我很感激有任何帮助。

另请注意,这不在画布页面上。

<?php
ob_start();
/**
 * Copyright 2011 Facebook, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

require_once('../facebook-php-sdk/facebook.php');

include('app3.conf.inc.php');

$facebook = new Facebook(array (
            'appId' => $appConf['appID'],
            'secret' => $appConf['appsecret'],
        ));

// Get User ID
$user = $facebook->getUser();

// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me?fields=permissions');
  } catch (FacebookApiException $e) {
     error_log($e);
    $user = null;
  }
}


// Login or logout url will be needed depending on current user state.
if ($user) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}

// This call will always work since we are fetching public data.
try
{
    $naitik = $facebook->api('/naitik');
}
catch (Exception $ex)
{
    echo("Exception with api/naitik:\n<pre>");
    var_dump($ex);
    echo("</pre>");
    die();
}

?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <head>
    <title>php-sdk</title>
    <style>
      body {
        font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
      }
      h1 a {
        text-decoration: none;
        color: #3b5998;
      }
      h1 a:hover {
        text-decoration: underline;
      }
    </style>
  </head>
  <body>
    <h1>php-sdk</h1>

    <?php if ($user): ?>
      <a href="<?php echo $logoutUrl; ?>">Logout</a>
    <?php else: ?>
      <div>
        Login using OAuth 2.0 handled by the PHP SDK:
        <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
      </div>
    <?php endif ?>

    <h3>PHP Session</h3>
    <pre><?php print_r($_SESSION); ?></pre>

    <h3>Cookies</h3>
    <pre><?php print_r($_COOKIE); ?></pre>


    <?php if ($user): ?>
      <h3>You</h3>
      <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">

      <h3>Your User Object (/me)</h3>
      <pre><?php print_r($user_profile); ?></pre>
    <?php else: ?>
      <strong><em>You are not Connected.</em></strong>
    <?php endif ?>

    <h3>Public profile of Naitik</h3>
    <img src="https://graph.facebook.com/naitik/picture">
    <?php echo $naitik['name']; ?>
  </body>
</html>

2 个答案:

答案 0 :(得分:2)

Facebook已经提出并接受了最新SDK的错误。我建议从2011年早些时候降级到一个。它解决了我的问题。

答案 1 :(得分:0)

好像你很亲密。在facebook的php配置下面,我提供了一个if GET查询来检测用户是否已注销。当他们注销时,条件中的代码会触发

unset($_COOKIE['fbsr_' . $config['appId']]);

而不是

unset($_COOKIE['fbsr_' . $this->getAppId()]);

这似乎是一种魅力。