How to skip the choose account pop-up with Google Sign In when requesting an additional scope

Viewed 974

I've integrated Google Sign In for web per https://developers.google.com/identity/sign-in/web/incremental-auth. After a user is signed in using the test code below, calling addScope() triggers the Choose Account pop up while requesting additional scope. How can one skip "Choose Account" and use the currently signed in user account? I've tried adding fields 'user_id' and 'login_hint' to the grant() configuration object without success.

<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 Answers
Related