Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Facebook已连接,但/me用户是空对象_Javascript_Facebook_Api_Undef - Fatal编程技术网

Javascript Facebook已连接,但/me用户是空对象

Javascript Facebook已连接,但/me用户是空对象,javascript,facebook,api,undef,Javascript,Facebook,Api,Undef,我使用了facebook文档中的示例,但在授予facebook连接权限后,我无法从用户对象获取任何用户信息。用户已定义,但其所有属性均为空。这是我的密码: <html> <head> <title>My Facebook Login Page</title> </head> <body> <div id="fb-root"></div> &

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

我的Facebook登录页面
window.fbAsyninit=函数(){
FB.init({
appId:'161718123880158',//应用ID
channelUrl:“”,//通道文件
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true,//解析xfbml
日志记录:正确
});
FB.api('/me',函数(用户){
如果(用户){
var image=document.getElementById('image');
image.src=http://graph.facebook.com/“+user.id+”/picture”;
var name=document.getElementById('name');
name.innerHTML=user.name
}
});
};
//异步加载SDK
(职能(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);
}(文件);
使用Facebook登录

名称和空图像的输出是未定义的,这是因为您没有活动的访问令牌。如果您执行了一个命令,则放置一个console.log(user)并检查响应,它将显示错误

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

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

  • 谢谢你,这就成功了。我不确定是哪一个解决了这个问题。我将“使用Facebook登录的网站”更新为我测试时使用的准确url,但没有在#2中添加FB.getLoginStatus,它仍然不起作用。登录是否适用于主域下的所有URL?例如,如果我们在设置中指明“example.com”,那么“example.com/mypage”也会起作用吗?