我使用以下代码在我的应用程序中实现了基于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帐户电子邮件地址,以及用户Google+个人资料中经过验证的公开电子邮件地址。您可以通过调用people.get来访问电子邮件地址,这将返回电子邮件数组。用户所属的Google Apps域名(如果有)。
如果你正在使用Rails,问题可能是你需要将omniauth-google-oauth2
更新为(0.6.0)
https://github.com/zquestz/omniauth-google-oauth2/issues/358
谷歌似乎改变了他们的API返回的内容。上述问题中的第一条评论显示哈希的结构已发生变化。
对于仍在寻找答案的人,请尝试使用此:
scope: 'openid profile email'