5

In order to make a request to Youtube Data API's youtube.channels.list with the mine parameter set to true, I have to make a properly authorized request, i.e. I need to provide a token that is bound to that channel.

In order to get that token, I use the Google client library from here:

<script src="//apis.google.com/js/client:platform.js"></script>

that enables me to make authentication requests on behalf of a certain Google account.

With this in mind, I have this code:

var parameters = {
  'clientid': 'CLIENT_ID',
  'scope': 'https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/youtube.readonly',
  'cookiepolicy': 'single_host_origin',
  'include_granted_scopes': true,
  'callback': 'googleLogin'
};

gapi.auth.signIn(parameters);

...

window.googleLogin = (authResult) => {
  if (authResult[ 'error' ] == 'immediate_failed') return;

  if (authResult[ 'status' ][ 'signed_in' ]) {
    // make GET request to https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true
    // with Authorization header `Bearer ${gapi.auth.getToken().access_token}`        
  } else {
    console.log("singned out");
  }
};

Now the problem is that after I choose my Google Account and let's say Youtube Account no. 2, I get the results as if I had chosen the main Youtube account.

So the problem here is that the token that I get from gapi.auth doesn't consider, in any way, what Youtube account I choose and it always generates tokens for the main account (no. 1).

  • I tried signing out before singing in, but doesn't do anything.
  • I've also tried using gapi.auth2, but with this one I get another annoying behaviour that I've described here: link

What's even more weird is that if I try switching between the Youtube accounts several times, it sometimes makes request for the correct account, so the code is correct, but still doesn't offer the desired result.

So how can I make proper authorized requests based on a certain Youtube account?

Community
  • 1
  • 1
Vlad
  • 997
  • 1
  • 5
  • 18

0 Answers0