He estado usando la API de datos de YouTube con el método de autenticación de inicio de sesión de Google oAuth durante meses. Estaba funcionando perfectamente. Pero durante muchos días, recibo advertencias en la consola de que las bibliotecas de autenticación antiguas quedarán obsoletas pronto. Así que seguí la documentación https://developers.google.com/identity/oauth2/web/guides/migration-to-gis para migrar a Google Identity Services para la autenticación. Obtuve correctamente el token de acceso, pero no puedo hacer que funcione con la API de datos de YouTube. Recibo el client:36 [GSI_LOGGER-TOKEN_CLIENT]: Set token failed. Gapi.client.setToken undefined. después de la autenticación y obtener el error Uncaught ReferenceError: gapi is not defined después de llamar a la función getChannel()
Su ayuda significaría mucho para mí.
var client; var access_token; function initClient() { client = google.accounts.oauth2.initTokenClient({ client_id: 'MY_CLIENT_ID-----------------au08jm.apps.googleusercontent.com', scope: 'https://www.googleapis.com/auth/youtube.readonly', callback: (tokenResponse) => { access_token = tokenResponse.access_token; gapi.client.setToken({ access_token: access_token }); }, }); } function getToken() { client.requestAccessToken(); } function revokeToken() { google.accounts.oauth2.revoke(access_token, () => {console.log('access token revoked')}); } channelForm.addEventListener('submit', e => { e.preventDefault(); const channel = channelInput.value; getChannel(channel); }); function getChannel(channel) { var myytid = channel; gapi.client.youtube.channels .list({ part: 'snippet,contentDetails,statistics', id: channel }) .then(response => { console.log(response); }) }Aquí están las bibliotecas y la función de llamada
<head> <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script> <script src="https://accounts.google.com/gsi/client" onload="this.onload=function(){};initClient()" async defer></script> </head> <body> <button onclick="getToken();">Get access token</button> <form id="channel-form-myyoutube"> <input type="text" placeholder="Enter Channel ID" id="channel-input-myyoutube"> <input type="submit" id="ytfetchbut" value="submit"> </form> </body>