【问题标题】:Facebook connected but /me user is an empty objectFacebook 已连接但 /me 用户是一个空对象
【发布时间】:2012-07-17 14:56:11
【问题描述】:

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

<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

【问题讨论】:

    标签: javascript facebook api undef


    【解决方案1】:

    这是因为您没有有效的访问令牌。如果你放一个 console.log(user) 并检查它显示错误的响应。

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

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

    【讨论】:

    • 谢谢你成功了。我不是 100% 确定哪一个解决了这个问题。我将“使用 Facebook 登录的网站”更新为我正在测试的确切 url,但没有在 #2 中添加 FB.getLoginStatus 它仍然无法正常工作。登录是否适用于主域下的所有 url?例如,如果我们在设置中指定“example.com”,那么“example.com/mypage”是否也能正常工作?
    猜你喜欢
    • 2019-12-21
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多