Facebook网站API:登录并显示用户图像和名称。活动访问令牌错误

时间:2012-07-13 14:51:16

标签: facebook facebook-graph-api

我的问题是显示一个登录按钮,并且(一旦用户登录)显示他的个人资料图片,姓名和“注销”链接。

在facebook api文档(这里是http://developers.facebook.com/docs/guides/web/#login)中,我找到了这个例子:

 <html>
  <head>
   <title>My Facebook Login Page</title>
  </head>
  <body>

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : 'YOUR_APP_ID', // App ID
          channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
          status     : true, // check login status
          cookie     : true, // enable cookies to allow the server to access the session
          xfbml      : true  // parse XFBML
        });
        FB.api('/me', function(user) {
          if (user) {
            var image = document.getElementById('image');
            image.src = 'http://graph.facebook.com/' + user.id + '/picture';
            var name = document.getElementById('name');
            name.innerHTML = user.name
          }
        });
      };
      // Load the SDK Asynchronously
      (function(d){
         var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement('script'); js.id = id; js.async = true;
         js.src = "//connect.facebook.net/en_US/all.js";
         ref.parentNode.insertBefore(js, ref);
       }(document));
    </script>

    <div align="center">
      <img id="image"/>
      <div id="name"></div>
    </div>

  </body>
</html>

那应该做我想要的。我用我的app_id和我的域名网站(http://localhost/index.html)替换了'MY_APP_ID'和MY_DOMAIN.COM。我还添加了一个登录按钮,就像我在文档中找到的那样(可以让我登录),但是没有显示图像或名称。我在控制台中编写了“user”对象并且它包含了这个:

error: Object
   code: 2500
   message: "An active access token must be used to query information about the current user."
   type: "OAuthException"

我应该如何使用这个“主动访问令牌”?

1 个答案:

答案 0 :(得分:3)

为了完成任何用户数据检索,每个Facebook应用程序必须首先对用户会话进行身份验证,以确保该应用程序具有最终用户检索其数据的权限。

参考https://developers.facebook.com/docs/authentication

使用Javascript SDK检索他们的访问令牌,您应该能够开始检索他们的用户显示名称和图像。

Facebook JS SDK https://github.com/facebook/facebook-js-sdk/