无法通过facebook api

时间:2016-10-07 11:14:19

标签: javascript facebook-graph-api facebook-javascript-sdk

我在下面使用:

window.fbAsyncInit = function() {
    FB.init({
        appId      : 'my-app-id',
        xfbml      : true,
        version    : 'v2.7'
    });
};

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
 js = d.createElement(s); js.id = id;
 js.src = "//connect.facebook.net/en_US/sdk.js";
 fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

并尝试获取以下电子邮件信息:

FB.api('/me?fields=name,email', function (response) {
     console.log(response.name + ' --- '+response.email);
});

输出结果是:Nitesh Kumar ---未定义

如何获取用户的电子邮件?

1 个答案:

答案 0 :(得分:0)

您需要在登录过程中询问电子邮件权限:

FB.login(function(response) {
    if (response.authResponse) {
        //user just authorized your app

    }
}, {scope: 'email'});
不过,这是正确的(包括最先进的ES6箭头功能):

FB.api('/me', {fields: 'name,email'}, (response) => {
     console.log(response.name + ' --- ' + response.email);
});

更多信息:http://www.devils-heaven.com/facebook-javascript-sdk-login/