Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

394
Visualizações
YouTube Data API with authentication issues in JS (Gapi Undefined)

I have been using YouTube Data API with Google Sign In authentication method oAuth for months. It was working perfectly. But for many days, I am getting warnings on console that old authentication libraries will be deprecated soon. So I followed the documentation https://developers.google.com/identity/oauth2/web/guides/migration-to-gis to migrate to Google Identity Services for authentication. I successfully get the access token, but cannot get it to work with YouTube Data API. I am getting the warning client:36 [GSI_LOGGER-TOKEN_CLIENT]: Set token failed. Gapi.client.setToken undefined. after authentication and getting the error Uncaught ReferenceError: gapi is not defined after calling the function getChannel()

Your help would mean a lot to me.

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);
    })
}

Here are the libraries & function call

<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>
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda