2013-05-21 237 views
1

我必须创建一个网站才能使用Facebook登录。有道理能够向他们发送电子邮件,但我无法从Facebook获取电子邮件地址。Facebook API获取电子邮件地址

我完全遵循https://developers.facebook.com/docs/facebook-login/getting-started-web/ 的指示我在FB.login中添加了{scope:email}。

如何使用Facebook API获取电子邮件地址?

我的代码现在:

<div id="fb-root"></div> 
<script> 

    window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : 'app', // App ID 
    channelUrl : '//domain.COM/channel.php', // Channel File 
    status  : true, // check login status 
    cookie  : true, // enable cookies to allow the server to access the session 
    xfbml  : true // parse XFBML 
    }); 

    FB.Event.subscribe('auth.authResponseChange', function(response) { 
    if (response.status === 'connected') { 


    FB.api('/me', function(response) { 

     console.log(response); 
    }); 

    } else if (response.status === 'not_authorized') { 

     FB.login(function(response) {},{scope: 'email'}); 

    } else { 


     FB.login(function(response) {}, {scope: 'email'}); 


    } 
    }); 
    }; 

    // 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> 
    <fb:login-button show-faces="true" width="200" max-rows="1"></fb:login-button> 

回答

0

如果您在成功登录后,调用response.email将返回用户的电子邮件地址。

+0

这是我没有电子邮件对象存在问题。在回应中,我只有基本知识。 – Alqin

+0

在连接了response.status的情况下console.log(response)的输出是什么? –

1

我遇到了同样的问题。取消fb帐户中的应用程序授权。然后通过您的测试页再次授权该应用程序

我发现,一旦我第一次授权应用程序,它就没有触及下面的块。 (添加范围:邮件到的FB.login一旦应用程序已被授权并没有好...我认为)

} else if (response.status === 'not_authorized') { 

    FB.login(function(response) {},{scope: 'email'}); 

} else { 

    FB.login(function(response) {}, {scope: 'email'}); 

} 

希望帮助

相关问题