Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Email scope is not working which is used in FB Login

I try to use FB Login on my website and need to get user informations ( email, public_profile, date of birth and location ). But after logged in only name and facebookId of user that I got. Codes :

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

Javascript part :

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 result :

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

No email or any other informations.

like image 262
OwnurD Avatar asked Sep 07 '15 10:09

OwnurD


1 Answers

That is a well known issue, search for "Declarative Fields" in the changelog: https://developers.facebook.com/docs/apps/changelog#v2_4

For example:

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

I believe you can also do it like this:

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

Btw, make sure you know how asynchronous JavaScript works, you are calling checkLoginState() right before FB.login, but FB.getLoginStatus does not return something immediately and you are always calling FB.login. Here´s an article about how to use FB.login and FB.getLoginStatus in a proper way: http://www.devils-heaven.com/facebook-javascript-sdk-login/

like image 82
andyrandy Avatar answered Oct 06 '22 19:10

andyrandy