简体   繁体   中英

google+ api login not returning email

I have implemented javascript based google+ login in my application using the following code:

var isGPInitialzed = false;

function render() {
    gapi.signin.render('loginWithGoogle', {
        'callback': 'onSignIn',
        'clientid': 'the client id',
        'cookiepolicy': 'single_host_origin',
        'requestvisibleactions': 'http://schema.org/AddAction',
        'scope': 'https://www.googleapis.com/auth/plus.login'
    });
    isGPInitialzed = true;
}


//Google 
function onSignIn(authResult) {
    if (!isGPInitialzed) {
        if (authResult['status']['signed_in']) { //get some user info
            gapi.client.load('oauth2', 'v2', function () {
                gapi.client.oauth2.userinfo.get().execute(function (response) {
                    console.log(response.email);
                    $.ajax({
                        url: '/Account/GLogin',
                        type: 'POST',
                        data: {
                            email: response.email,
                            name: response.name,
                            profilePicture: response.picture
                        },
                        dataType: 'json',
                        success: function (isUserLoggedIn) {
                            if (isUserLoggedIn) {
                                window.location.reload();
                            }
                        }
                    });
                });
            });
        }
    }
    else {
        isGPInitialzed = false;
    }
};

It was working fine until I created a new application from another account and replaced the client id. On successful authentication, the api is not returning the user email in the response. I have checked in the google+ account settings for the Apps and there is not setting to give acces to the email. What can be the issue?

改变范围

'scope': 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email',

based on latest update from google developers, please change the scope,

https://www.googleapis.com/auth/plus.profile.emails.read

This scope requests that your app be given access to: the user's Google account email address, as well as any public, verified email addresses in the user's Google+ profile. You access the email addresses by calling people.get, which returns the emails array. the name of the Google Apps domain, if any, that the user belongs to.

If you're using Rails, the issue might be that you need to update omniauth-google-oauth2 to (0.6.0)

https://github.com/zquestz/omniauth-google-oauth2/issues/358

Google seems to have changed what gets returned by their API. The first comment in the above issue shows the structure of the hash has changed.

对于仍在寻找答案的人,请尝试使用此:

 scope: 'openid profile email'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM