facebook connect不会返回电子邮件地址

时间:2016-02-17 22:08:14

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

我正在使用这个简单的FB连接

<a href="#" onclick="fb_login();">Connect</a>

window.fbAsyncInit = function(){     FB.init({         appId:&#39; YOUR_APP_ID&#39;,         oauth:是的,         status:true,//检查登录状态         cookie:true,//启用cookie以允许服务器访问会话         xfbml:true //解析XFBML     });

};

function fb_login(){
    FB.login(function(response) {

        if (response.authResponse) {
            console.log('Welcome!  Fetching your information.... ');
            console.log(response); // dump complete info
            access_token = response.authResponse.accessToken; //get access token
            user_id = response.authResponse.userID; //get FB UID

            FB.api('/me', function(response) {
                user_email = response.email; //get user email
                console.log(response); 

            });

        } else {
            //user hit cancel button
            console.log('User cancelled login or did not fully authorize.');    
        }
    }, {
        scope: 'email'
    });
}
(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

当我连接时,它只返回我

  

对象{名称:&#34;我的名字在这里&#34;,id:&#34; 102xxxxxxxx230&#34;}

如何检索电子邮件地址?

1 个答案:

答案 0 :(得分:5)

FB.api('/me', {fields: 'name,email'}, function(response) {
    user_email = response.email; //get user email
    console.log(response); 
});

它被称为“声明字段”,请参阅changelog:https://developers.facebook.com/docs/apps/changelog#v2_4