我已根据https://developers.google.com/identity/sign-in/web/incremental-auth集成了Google登录网页 . 用户使用下面的测试代码登录后,调用addScope()会在请求其他范围时触发选择帐户弹出 . 如何跳过"Choose Account"并使用当前登录的用户帐户?我've tried adding fields ' user_id ' and ' login_hint'到grant()配置对象没有成功 .
<script src="https://apis.google.com/js/platform.js"></script>
<div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
function initClient() {
gapi.load('auth2', function () {
gapi.auth2.init({
client_id: 'CLIENT_ID.apps.googleusercontent.com',
fetch_basic_profile: true
});
});
};
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId());
console.log("Email: " + profile.getEmail());
addScope();
}
function addScope() {
var options = new gapi.auth2.SigninOptionsBuilder({ 'scope': 'email https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/contacts.readonly' });
var auth2 = gapi.auth2.getAuthInstance();
var 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 }));
});
}
initClient();
1 回答
不推荐,但您可以这样做:
见https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleauthsignin