我在我的网站中使用 Google 登录按钮。我能够授权用户并获取用户的个人资料信息。
<div id="my-signin2" ></div>
gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'light',
'onsuccess': this.onSuccess,
'onfailure': this.onFailure
});
onSuccess(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
var name = googleUser.getBasicProfile().getName());
var email = googleUser.getBasicProfile().getEmail());
var imageUrl = googleUser.getBasicProfile().getImageUrl());
}
onFailure(error) {
}
现在我需要在他授权使用谷歌时阅读用户联系人。
因此,为了获取联系人,我添加了一个范围
https://www.googleapis.com/auth/contacts.readonly
gapi.signin2.render('my-signin2', {
'scope': 'profile email https://www.googleapis.com/auth/contacts.readonly',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'light',
'onsuccess': this.onSuccess,
'onfailure': this.onFailure
});
每件事都运行良好。用户使用 gmail 授权后,它会向用户显示权限屏幕并请求权限。
但是当用户拒绝权限时,它甚至没有记录用户。即使他拒绝,我也希望用户登录,如果我没有得到他们的联系人也没关系。
我已阅读有关请求额外范围的信息。
var options = new gapi.auth2.SigninOptionsBuilder(
{'scope': 'https://www.googleapis.com/auth/contacts.readonly'});
googleUser = auth2.currentUser.get();
googleUser.grant(options).then(
function(success){
console.log(JSON.stringify({message: "success", value: success}));
},
function(fail){
alert(JSON.stringify({message: "fail", value: fail}));
});
当我在 onSuccess(用户授权电子邮件后)之后使用它时,它总是会使用失败方法。给出错误“ popup_closed_by_user ”