如何在请求其他范围时跳过使用Google登录选择帐户弹出窗口

时间:2017-05-30 16:46:45

标签: google-signin google-oauth2 googlesigninapi

我已根据https://developers.google.com/identity/sign-in/web/incremental-auth整合了Google登录网络。 用户使用下面的测试代码登录后,调用addScope()会在请求其他范围时触发选择帐户弹出。如何跳过“选择帐户”并使用当前登录的用户帐户?我尝试将字段'user_id'和'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 个答案:

答案 0 :(得分:1)

不推荐,但你可以这样做:

this.auth2.signIn({ "prompt": "none" }).then(

请参阅https://developers.google.com/api-client-library/javascript/reference/referencedocs#googleauthsignin

相关问题