如何使用JavaScript SDK获取Facebook用户的电子邮件地址

33

我正在使用JavaScript API为Facebook创建我的应用程序。问题是,它返回了
email = undefined

我不知道为什么?如果我在我的应用程序上使用Facebook登录/注销按钮,那么警报将显示用户的正确电子邮件地址,但我不想这样做。 我错过了什么吗?

以下是我的代码:

<p><fb:login-button autologoutlink="true" perms="user_about_me,email"></fb:login-button></p>

<script>
window.fbAsyncInit = function () {
  FB.init({ appId: '250180631699888', status: true, cookie: true,
  xfbml: true
});

  FB.getLoginStatus(function (response) {
    if (response.session) {
      greet();
    }
  });
};
(function () {
  var e = document.createElement('script');
  e.type = 'text/javascript';
  e.src = document.location.protocol +
  '//connect.facebook.net/en_US/all.js';
  e.async = true;
  document.getElementById('fb-root').appendChild(e);
} ());

function greet() {
  FB.api('/me', function (response) {
  alert('Welcome, ' + response.name + "!");
  alert('Your email id is : '+ response.email);
});
}
</script>
6个回答

107
// https://developers.facebook.com/docs/javascript/reference/FB.api/
// v2.4

FB.api('/me', { locale: 'en_US', fields: 'name, email' },
  function(response) {
    console.log(response.email);
  }
);

1
这就是帮助了我。 - Benjamin
为什么FB的人们一直在胡闹……?!?!谢谢@masakielastic!我找了好几个小时才找到你的答案! - Pedro Ferreira
要明确的是,如果您这样做并且没有收到电子邮件,请确保调用FB.login(...,{scope:"email"})。在v6中必须这样做。 - Judah Gabriel Himango

16

下面是我获取用户名和邮箱的示例:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>

<script>
  $(function() {
    FB.init({
      appId  : 'APP_ID',
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });

    FB.getLoginStatus(function(response) {
      if (response.status == 'connected') {
        getCurrentUserInfo(response)
      } else {
        FB.login(function(response) {
          if (response.authResponse){
            getCurrentUserInfo(response)
          } else {
            console.log('Auth cancelled.')
          }
        }, { scope: 'email' });
      }
    });

    function getCurrentUserInfo() {
      FB.api('/me', function(userInfo) {
        console.log(userInfo.name + ': ' + userInfo.email);
      });
    }
  });
</script>

1
对我来说只是缺少了 { scope: 'email' }。 - Thalis Vilela

9
<button id="fb-login">Login & Permissions</button>

<script>
document.getElementById('fb-login').onclick = function() {
  var cb = function(response) {
    Log.info('FB.login callback', response);
    if (response.status === 'connected') {
      Log.info('User logged in');
    } else {
      Log.info('User is logged out');
    }
  };
  FB.login(cb, { scope: 'email' });
};
</script>

使用此功能可获得额外的权限。

有关更多详细信息,请访问: https://www.fbrell.com/examples/


1
, { scope: 'email' } , 挽救了我的生命。尝试了很多...只有这个解决方案对我起作用。 - Vikrant
1
{ scope: 'email' } 是我缺失的部分。谢谢! - zoan

9

谢谢您的回复,我还有一个问题,如何获取用户登录时使用的电子邮件地址?一个个人资料可能会有其他相关的电子邮件地址... - user958414
你需要使用你的应用访问令牌而不是用户的访问令牌,然后使用它来检索所需的信息。 - TommyBs

1
在这段代码中,我从Facebook获取用户数据并使用ajax将其存储到我的数据库中。
 FB.login(function(response) {
          if (response.authResponse) {
         FB.api('/me?fields=email,name,first_name,last_name', function(response) 
         {
             FB.api(
             "/"+response.id+"/picture?height=100",
                                    function (responses) {
                                        //console.log(responses.data.url)
                                                    response['profile_pic']=responses.data.url;
                                        $.ajax({
                                                   type:"POST",
                                                   url:'<?php echo base_url(); ?>'+'home/facebook_get_signup',
                                                   data:response,
                                                   success:function(res)
                                                   {
                                                       if(res=='success')
                                                       {

                                                           window.location='<?php echo base_url(); ?>';
                                                       }
                                                       if(res=='exists')
                                                       {
                                                           window.location='<?php echo base_url(); ?>';  
                                                       }
                                                   }
                                               });

                                    }
                                )
         });
        } else {
         console.log('User cancelled login or did not fully authorize.');
        } 
      // handle the response
        }, {scope: 'email,user_likes'});

0

你的解决方案存在一些问题。首先,你正在使用旧的身份验证方案。你应该使用这里描述的新方案: https://developers.facebook.com/docs/reference/javascript/

你需要在你的init函数中添加oauth:true,并确保你的getLoginStatus寻找新类型的响应。

说到这里,你需要确保你有正确的权限来查看用户的电子邮件。你可以在这里查看必需的权限: http://developers.facebook.com/docs/reference/api/user/

通过使用TommyBs在另一个答案中描述的FB.login函数来获取这些。

一旦你拥有这些选项,就可以使用FB.api函数获取电子邮件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接