google+ api 登录未返回电子邮件

google+ api login not returning email

我已经使用以下代码在我的应用程序中实现了基于 javascript 的 google+ 登录:

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

在我从另一个帐户创建新应用程序并替换客户端 ID 之前,它工作正常。身份验证成功后,api 不会在响应中返回用户电子邮件。我已经检查了应用程序的 google+ 帐户设置,但没有设置允许访问电子邮件。可能是什么问题?

更改范围

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

根据 google 开发人员的最新更新,请更改范围,

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

此范围请求授予您的应用访问权限: 用户的 Google 帐户电子邮件地址,以及用户 Google+ 个人资料中任何 public 已验证的电子邮件地址。您可以通过调用 people.get 来访问电子邮件地址,其中 returns 电子邮件数组。 用户所属的 Google Apps 域的名称(如果有)。

如果您使用的是 Rails,问题可能是您需要将 omniauth-google-oauth2 更新到 (0.6.0)

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

Google 似乎已经改变了他们的 API 返回的内容。上期第一条评论显示hash的结构发生了变化

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

 scope: 'openid profile email'