-1

以下控制台记录“必须使用活动访问令牌来查询有关当前用户的信息。” 显然我的 js 没有从 php 获取访问令牌 - 我该如何纠正这个问题?下面的代码是 FB 在他们的博客上发布的内容的虚拟克隆: https ://developers.facebook.com/blog/post/534/

<?php
$basePath = "/usr/lib/fb";

// Include the fb sdk
require 'fb/src/facebook.php';

// Create our Application instance
$facebook = new Facebook(array(
  'appId'  => '#####', // I do repeat this in fb.js so I can externalize it
  'secret' => '#####',
));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}

?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
  <body>
    <?php if ($user_profile) { ?>
      Your user profile is 
      <pre>            
        <?php print htmlspecialchars(print_r($user_profile, true)) ?>
      </pre> 
    <?php } else { ?>
      <fb:login-button></fb:login-button>
    <?php } ?>
    <div id="fb-root"></div>
    <script>               
      window.fbAsyncInit = function() {
        FB.init({
          appId: '<?php echo $facebook->getAppID() ?>', 
          cookie: true, 
          xfbml: true,
          oauth: true
        });
        FB.Event.subscribe('auth.login', function(response) {
          window.location.reload();
        });
        FB.Event.subscribe('auth.logout', function(response) {
          window.location.reload();
        });

        FB.api('/me/permissions', function (response) {
            console.log(response);
        } );

      };


      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>
  </body>
</html>
4

2 回答 2

1

您加载并启动了 fb sdk,但您需要对用户进行身份验证。
仅对于 js 身份验证,您需要使用FB.login 方法,但如果您的 php 负责对用户进行身份验证,那么您只需调用FB.getLoginStatus,例如:

window.fbAsyncInit = function() {
    FB.init({
        appId: '<?php echo $facebook->getAppID() ?>', 
        cookie: true, 
        xfbml: true,
        oauth: true
    });
    FB.Event.subscribe('auth.login', function(response) {
        window.location.reload();
    });
    FB.Event.subscribe('auth.logout', function(response) {
        window.location.reload();
    });

    FB.getLoginStatus(function(response1) {
        if (response1.status === 'connected') {
            FB.api('/me/permissions', function(response2) {
                console.log(response2);
            });
        }
        else {
            // user is not logged in or not authorized with the app.
            // use a login button or FB.login
        }
    });
};
于 2012-06-27T07:51:08.230 回答
0

me/permissions即使用户没有授权您的应用程序,您也在打电话,

改为这样做,

<?php if ($user_profile): ?>
   FB.api('/me/permissions', function (response) {
      console.log(response);
   });
<?php endif; ?>
于 2012-06-27T08:00:11.373 回答