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":""}
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)
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.