Facebook oauth retrieve user Email

前端 未结 4 1633
半阙折子戏
半阙折子戏 2020-12-13 12:29

I am using Facebook Javascript SDK to get oauth token and retrieve all the data from FB:

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


        
相关标签:
4条回答
  • 2020-12-13 13:06

    It is impossible, user have to grant your application to access his email information, or in other case, it can be accessible if user allowed his email to viewed by everybody in his privacy settings.

    0 讨论(0)
  • 2020-12-13 13:24

    First make your app live, available to public. Then you have to grant permission to get user 'email' which is not allowed default by the app. After that the login method should be changed as follows.

    function checkLoginState() {
        FB.getLoginStatus(function(response) {
            statusChangeCallback(response);
            console.log('Welcome!  Fetching your information.... ');
            var url = '/me?fields=id,name,email';
            FB.api(url, function(response) {
                 alert(response.name + " " + response.id + " " +response.email);
            }, {scope: 'email'});
        });
    }
    

    If the field id is not needed change the variable url as :

    var url = '/me?fields=name,email';
    
    0 讨论(0)
  • 2020-12-13 13:24

    If you can change to use a promise chaining, this worked for me:

    fb.login({scope:'email'}).then(function (resp) {
        if (resp.status === 'connected') {
           fb.api('/me', 'get', {fields:'id,email,first_name,gender,...'}).then(function (response) {
              alert('You have successfully logged in, ' + response.name + ", " + response.email + "!");
           });
        }
    });
    
    0 讨论(0)
  • 2020-12-13 13:30

    You need get email explicitly as follows:

                        var url = '/me?fields=name,email';
                        FB.api(url, function (response) {
                            alert(response.name);
                            alert(response.email);
                        });
    

    And to get the email address, email permission is needed. So the code might look like (there are other options like Register button, login button..)

                FB.login(function (response) {
                    if (response.session) {
                        var url = '/me?fields=name,email';
                        FB.api(url, function (response) {
                            alert(response.name);
                            alert(response.email);
                        });
                    }
                    else {
                        alert("User did not login successfully");
                    }
                }, { scope: 'email' }); /* perms changed to scope */
    
    0 讨论(0)
提交回复
热议问题