App Access Token is Undefined

Hi All, Extension noob here. Ultimately I’d like to query my Extension users to get their display names. I understand I can do this once I’ve got my app access token. My EBS is on Firebase functions and I’ve already succeeded JWT signing for other functionailty, so my creds / IDs should be OK…

I’ve tried extracting the pertinent parts of the app access retrieval from here:

But the access_token in the fetch response is always undefined.

  let token_url = new URL('https://id.twitch.tv/oauth2/token');
      token_url.search = new URLSearchParams([
          [ 'client_id',      config.client_id ],
          [ 'client_secret',  config.client_secret ],
          [ 'grant_type',    'client_credentials' ]
      ]).toString();

      functions.logger.log('token_url ' , token_url );
      functions.logger.log('token_url.search ' , token_url.search );

      fetch(
          token_url,
          {
              method: 'POST',
              headers: {
                  'Accept': 'application/json'
              }
          }
      ).then(token_resp => {

        functions.logger.log('response ' , token_resp );

        let token_body = token_resp.json().then( respJson =>{

          config.api_token = respJson.access_token;
          functions.logger.log('Got a App Access Token', config.api_token);
          functions.logger.log('Ready to start');
  
          const resp = { "user_access_token" : config.api_token };
          res.status(200).send(resp);

        })

      });

I’d be grateful for a steer!
Thanks!

You didn’t test the HTTP response from token_resp
And jumped straight to JSON parsing it
And assuming it was successful.

So log respJson inside

          functions.logger.log('RawResponse', respJson);

          config.api_token = respJson.access_token;
          functions.logger.log('Got a App Access Token', config.api_token);

I’d specualate you have use the extension secret instead of the extension Client secret/Twitch API Client Secret

Which is what the respJson would tell you (invalid secret)

This is covered in the ReadMe here →

Thanks for the tip-off. Indeed I was using the wrong secret. To compound the issue, the .json() promise was returning “pending” as I hadn’t wrapped it properly. Working code:

fetch(
          token_url,
          {
              method: 'POST',
              headers: {
                  'Accept': 'application/json'
              }
          }
      ).then( token_resp => {

        functions.logger.log('token_resp RawResponse ' , token_resp );
        return token_resp.json();
      }).then(  respJson =>{

        functions.logger.log('respJson RawResponse ', respJson);

        config.api_token = respJson.access_token;
        functions.logger.log('Got a App Access Token', config.api_token);
        functions.logger.log('Ready to start');

        const resp = { "user_access_token" : config.api_token };
        res.status(200).send(resp);

      }

Thanks again!