从Facebook JS API,问题或功能获取电子邮件?

时间:2015-09-03 07:15:26

标签: javascript facebook facebook-javascript-sdk

我使用以下内容作为参考:

https://developers.facebook.com/docs/facebook-login/web

但是,我似乎无法提取电子邮件。我不确定这是因为用户设置不允许发送电子邮件,还是这与我正在进行的通话有关。

以下是我使用的代码:

function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }


function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);
    // The response object is returned with a status field that lets the
    // app know the current login status of the person.
    // Full docs on the response object can be found in the documentation
    // for FB.getLoginStatus().
    if (response.status === 'connected') {
      // Logged into your app and Facebook.
      testAPI();
    } else if (response.status === 'not_authorized') {
      // The person is logged into Facebook, but not your app.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into this app.';
    } else {
      // The person is not logged into Facebook, so we're not sure if
      // they are logged into this app or not.
      document.getElementById('status').innerHTML = 'Please log ' +
        'into Facebook.';
    }
  }

function testAPI() {
    console.log('Welcome!  Fetching your information.... ');
    FB.api('/me', function(response) {

      console.log('Successful login for: ' + response.name);
      document.getElementById('status').innerHTML =
        'Thanks for logging in, ' + response.name + '!';
      var result = JSON.stringify(response);
      alert(response.email)

    }, {scope: 'public_profile,email,first_name,last_name'});


<fb:login-button data-scope="email" scope="public_profile, email" onlogin="checkLoginState();">


</fb:login-button>

我可以登录,但我没有收到任何电子邮件。我不知道这是用户设置问题还是代码问题。

1 个答案:

答案 0 :(得分:3)

FB.api没有范围参数,您已经在登录按钮中定义了范围。也没有名为“first_name”或“last_name”的范围,这些只是用户表中的字段。

无论如何,从v2.4开始,你必须指定API端点中的字段:https://developers.facebook.com/docs/apps/changelog#v2_4

例如:

FB.api('/me', {fields: 'email,first_name,last_name'}, function(response) {
  ...
});

您可以在文档中详细了解FB.api:https://developers.facebook.com/docs/javascript/reference/FB.api

顺便说一下,如果您需要更多信息,还有一篇关于使用JS SDK登录的文章:http://www.devils-heaven.com/facebook-javascript-sdk-login/