Google Sign-In gives error when swiching to second

2019-04-06 07:31发布

问题:

I am currently trying to use gapi.auth2 from Google Sign-In for Websites API and this is the code I have:

-- load the library with:

<script src="https://apis.google.com/js/platform.js?onload=onLoadGapiCallback" async defer></script>

-- initialize an auth2 variable:

var auth2;

window.onLoadGapiCallback = () => {
  gapi.load('auth2', () => {
    auth2 = gapi.auth2.init({
      'client_id': 'CLIENT_ID',
      'scope': 'profile email https://www.googleapis.com/auth/youtube.readonly'
    });
  });
};

-- and when a botton is clicked do:

auth2.signIn().then(() => {
  console.log('auth is:', auth2.currentUser.get().getAuthResponse().access_token);
});

This works well, it initializes the auth2 variable, when I click the button, it shows the SingIn prompt and I choose one of my Google Accounts. The problem is from now on when I have to choose a YouTube account, if I choose other account than the main one, I'll get an Exception Object like this one:

{type: "tokenFailed", idpId: "google", error: "USER_LOGGED_OUT"}

also there's an XHR request being sent lastly that has this response:

{"error":"USER_LOGGED_OUT","detail":"No active session found."}

So it only works if I choose the main account, but I cannot choose other YouTube accounts.

What am I missing here? I've looked into all these docs but none helped me:

Getting profile information

Google Sign-In JavaScript client reference

Monitoring the user's session state

Update: Running the code from this example (but with this scope: 'profile email https://www.googleapis.com/auth/youtube.readonly') will only work if I choose the first Youtube account for each Google account. If I choose any other Youtube account, I'll get this alert error: