在我的网站上通过Facebook登录时如何获取用户的电子邮件

时间:2018-07-11 09:13:32

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

我已使用户能够通过社交媒体Facebook和google登录。用户正在通过Google登录,但无法通过Facebook登录。当我调试代码时,我知道用户的电子邮件值没有到来。但是用户名即将到来。因此,问题是如何获取用户的电子邮件。我对于Google和Facebook具有相同的代码。这是我的代码:

// get social contacts with this function.(like facebook,google,yahoo)
    function getLogin(network, path){
        var basePath = $('#baseurl').val(); // base url
        var redirectUrl = basePath+'login';
        var localScope = 'email';
        // initilize 
        hello.init({
            google : 'xxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxmbesm1.apps.googleusercontent.com',
            facebook : 'xxxxxxxxxxxxxxxxxx', 
            }, {
            redirect_uri: redirectUrl,
            scope:localScope
            }
        );
        // login
        hello.login( network, {scope:localScope}, function(auth){
            if(!auth||auth.error){  
                return;
            }
            else
            {
                // Get Profile
                hello( network ).api( '/me' , function responseHandler(p){
                    user    =   p;
                    var userstr="name="+user.name+"&";

                    userstr +="link="+''+"&";   
                    userstr +="username="+user.displayName+"&";
                    userstr +="socailid="+user.id+"&";   //str +="username="+user.given_name+"&";
                    userstr +="email="+user.email;
                    // save data to google
                    $.post(baseURL+'social_check', {email:user.email,socialid:user.id}, function(resp){
                        if(resp === 'true')
                        {
                            window.location.href = baseURL+'join';
                        }
                        else
                        {
                            $.ajax({
                                type: "POST",
                                url: baseURL+'social-signup',
                                dataType: 'json',
                                data: userstr,
                                success: function(response){

                                    if(response.success == 'inactive')
                                    {
                                        $('#gmail-error').show().html("Your account is inactive. To activate it, please click on the activation link we sent you by email. Want us to <a href='"+baseURL+"resend-confirmation-link'>resend it</a>?");
                                        $('#fb-error').hide();
                                        return false;
                                    }
                                    else if(response.success == 'deleted'){
                                        $('#gmail-error').show().html("Your account is inactive. To activate it, please click on the activation link we sent you by email. Want us to <a href='"+baseURL+"resend-confirmation-link'>resend it</a>?");
                                        $('#fb-error').show();
                                        return false;
                                    }
                                    else if(response.success=='error')
                                    {
                                        $('#gmail-error').show().text("We don't recognize this account - please sign up with it on the sign-up screen first.");
                                        $('#fb-error').hide();
                                        return true;
                                    }
                                    else if(response.success == 'not-inserted'){
                                        $('#gmail-error').show().text("Something went wrong.");
                                        $('#fb-error').hide();
                                        return false;
                                    }
                                    // if redirect url is not set
                                    if(response.success===false)
                                    {
                                        $('#fb-root').hide(); $('#status').hide();
                                        window.location.href = baseURL;
                                    }
                                    else{
                                        $('#fb-root').hide(); $('#status').hide();
                                        window.location.href = baseURL+response.success;
                                    }
                                },

                                error: function(response)
                                {
                                    $('#gmail-error').show().text("Oops something went wrong. Please try one more time.");
                                    $('#fb-error').hide();
                                    return false;
                                }
                            });
                        }
                    });
                }); 
            }
        });
    }
</script>

userstr +="email="+user.email;这是空白。我该怎么办才能收到用户的电子邮件。任何帮助都是非常可观的。如果有人有想法,请帮助我。

0 个答案:

没有答案