必须使用活动访问令牌来查询有关当前用户的信息

时间:2011-09-08 01:39:40

标签: php facebook facebook-access-token

我已经定义了一个访问令牌。它是一个用curl获得的新访问令牌。为什么我仍然收到错误:必须使用活动访问令牌来查询有关当前用户的信息。

<?php

define('YOUR_APP_ID', '*****');
define('YOUR_APP_SECRET', '*****');

function get_facebook_cookie($app_id, $app_secret) {
  $args = array();
  parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
  ksort($args);
  $payload = '';
  foreach ($args as $key => $value) {
    if ($key != 'sig') {
      $payload .= $key . '=' . $value;
    }
  }
  if (md5($payload . $app_secret) != $args['sig']) {
    return null;
  }
  return $args;
}

$cookie = get_facebook_cookie(YOUR_APP_ID, YOUR_APP_SECRET);

$url = "https://graph.facebook.com/oauth/access_token";
$client_id = "214620421927216";
$client_secret = "1dc2110b1cdbbdf8604edc4ee7b03b81";
$postString = "client_id=$client_id&client_secret=$client_secret&type=client_cred";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FAILONERROR, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
$response = curl_exec($curl);
print_r($response);
echo "</br>";

$access_token = substr($response,13);
echo $access_token;
echo "</br>";

$url = 'https://graph.facebook.com/me?access_token=' . $access_token;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);

$user = json_decode($response);
print_r($user);

?>
<html>
  <body>
        <?php if ($cookie) { ?>
      Welcome <?php  ?>
    <?php } else { ?>
      <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init({appId: '<?= YOUR_APP_ID ?>', status: true,
               cookie: true, xfbml: true});
      FB.Event.subscribe('auth.login', function(response) {
        window.location.reload();
      });
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:2)

您尝试获取的令牌是应用级访问令牌。 为了通过图形api查询“/ me”,您需要获取用户访问令牌。

检查FB doc中的用户登录部分:http://developers.facebook.com/docs/authentication/