Api.twitch.tv/helix/streams?first=10 returns 404 not found

I used to fetch a list of game streams streams on google apps script through the old kraken api.
It was set to run every 5 minutes, save a list of all streams from various niche games I’m interested in and then push any new streams as a notification to me elsewhere.

This as context ahead, in case it reveals me picking the wrong auth flow or you know a simpler/existing solution for this function.

In any case I want it running again through the helix api - and it seems I can get the required auth token, but then trying to use it to fetch streams just returns nothing.

  var client_id = 
  var client_secret = 
  var redirect = 'http://localhost:3000';

  var url = 'https://id.twitch.tv/oauth2/token'
  var header = {
    "client_id": client_id,
    "client_secret": client_secret,
    "redirect_uri": encodeURIComponent(redirect),
    "response_type": "token",
    "grant_type": "client_credentials"
  };
  var options = {
    'method' : 'post',
    'payload' : header,
  };
  var response = UrlFetchApp.fetch(url,options);
  var stock = JSON.parse(response.getContentText());
  var token_type = stock["token_type"];
  var access_token = stock["access_token"];

  console.log(token_type);
  console.log(access_token);

  url = 'https://api.twitch.tv/helix/streams?first=10'
  header = {
    'Client-ID': client_id,
    'Authorization': 'Bearer ' + access_token
  };
  options = {
    'method' : 'get',
    'payload' : header,
    'muteHttpExceptions': true
  };

  response = UrlFetchApp.fetch(url,options);
  console.log(response.getContentText());

The last output there is {"error":"Not Found","status":404,"message":""}

Your code broadly looks correct

You do not need

    "redirect_uri": encodeURIComponent(redirect),
    "response_type": "token",

on your token generator. See Getting OAuth Access Tokens | Twitch Developers

You also call the varable in your code header but it’s post data (not a issue just a confusing name)

It’s just client_id/client_secret/grant_type

But if you are getting a token then all good.

So it would seem that whatever UrlFetchApp.fetch is is messing about and not doing what we think it’s doing.

So your code seems ok, but if it’s getting a 404 on the second call (and not the first with the token gen). then seems there might be a library issue

1 Like

Thanks! I removed/fixed the confusing stuff there.

I hadn’t specifically picked a library myself and just let apps script deal with that - hoping it should work on the second response once I could get the token from the first.

How would I troubleshoot a library issue myself?

I’ve looked at the documentation here and just tried two things:
manually adding the required scope to the appsscript.json
and manually including the OAuth2 for Apps Script library
(though as far as I can tell that’s for the other end of creating/authorising tokens - and both of these things are meant to be automated)

Neither of that changed the outcome.

  • Get Streams doesn’t take any scopes
  • Client Credentials tokens can’t have scopes

Looks like you want a call along the lines of

  response = UrlFetchApp.fetch('https://api.twitch.tv/helix/streams?first=10', {
    'method' : 'get',
    'headers' : {
      'Client-ID': client_id,
      'Authorization': 'Bearer ' + access_token
    },
    'muteHttpExceptions': true
  );
  console.log(response.getContentText());

So the options you were passing to URLFetch are wrong.

You tried a send a payload which for this library is for POST BODY data, which might have caused Google’s URLFetch to auto convert from a GET request to a POST request, hence the 404 as you cannot POST to Get Streams.

1 Like

That works!

Missing a closing curly brace for the options, but it works :smile:

Thank you.

:+1: hadn’t tested it since I don’t use googles stuff so couldn’t easily test :stuck_out_tongue:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.