电子邮件范围无法在FB登录中使用

时间:2015-09-07 10:38:55

标签: javascript facebook-javascript-sdk facebook-login

我尝试在我的网站上使用FB登录,并需要获取用户信息(电子邮件,public_profile,出生日期和地点)。但是在登录后我只获得了用户名和facebookId。代码:

<img src="" alt="" onclick="fblogin();"/>

Javascript部分:

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.';
        }
    }

    // This is called with the results from from FB.getLoginStatus().
    function checkLoginState() {
        FB.getLoginStatus(function (response) {
            statusChangeCallback(response);
        });
    }



    function fblogin()
    {
        checkLoginState();
        FB.login(function (response) {
            if (response.authResponse) {
                console.log('Bilgiler Alınıyor');
                FB.api('/me', function (response) {

                    console.log(JSON.stringify(response));
                });
            } else {
                console.log('User cancelled login or did not fully authorize.');
            }
        }, { scope: 'user_about_me,email,public_profile' });
    }


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

        FB.getLoginStatus(function (response) {
            if (response.status === 'connected') {
                console.log(response.authResponse.accessToken);
            }
        });
    };

    (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'));

JSON结果:

{"name":"Name Surname","id":"xxxxxxxxxxxxxxxxx"}

没有电子邮件或任何其他信息。

1 个答案:

答案 0 :(得分:16)

这是一个众所周知的问题,在更改日志中搜索“声明字段”:https://developers.facebook.com/docs/apps/changelog#v2_4

例如:

FB.api('/me?fields=id,name,email', ...

我相信你也可以这样做:

FB.api('/me', {fields: 'id,name,email'}, ...

顺便说一句,确保你知道异步JavaScript是如何工作的,你在FB.login之前调用checkLoginState(),但FB.getLoginStatus不会立即返回一些东西而你总是调用FB.login。这篇文章是关于如何以正确的方式使用FB.login和FB.getLoginStatus:http://www.devils-heaven.com/facebook-javascript-sdk-login/

相关问题