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

Facebook connected but /me user is an empty object

本文关键字:一个 对象 连接 我的 用户 Facebook      更新时间:2023-11-18

我使用了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

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

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

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