Facebook连接但/我用户是一个空对象

时间:2012-07-17 14:56:12

标签: facebook api connect undef

我使用了facebook文档中的示例,但在授予facebook连接权限后,我无法从用户对象获取任何用户信息。定义了用户,但其所有属性都为null。这是我的代码:

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

      <div id="fb-root"></div>
      <script>
        window.fbAsyncInit = function() {
          FB.init({
            appId      : '161718123880158', // App ID
            channelUrl : '', // Channel File
            status     : true, // check login status
            cookie     : true, // enable cookies to allow the server to access the session
            xfbml      : true,  // parse XFBML
            logging     : true
          });
          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 class="fb-login-button" scope="email,user_checkins">Login with Facebook</div>
      <div align="center">
        <img id="image"/>
        <div id="name"></div>
      </div>

    </body>
 </html>

名称和空图像的输出为undef

1 个答案:

答案 0 :(得分:0)

这是因为您没有活动的访问令牌。如果您执行了放置console.log(用户)并检查响应,则会显示错误。

  1. app settings中设置的“有Facebook登录的网站”字段是什么?它需要匹配您正在开发的URL。
  2. 尝试在FB.getLoginStatus回调函数中包装FB.api调用,如下所示:

    FB.getLoginStatus(function() {
       FB.api('/me', function(user) {
          //your code here
       });
     })