Detect if a streamer is live (Twitch Helix API)

ThommyGames06 Posted messages 5 Status Member -  
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   -
Bonjour,

I am trying to create a code that can detect when a streamer is online on Twitch using the Twitch API "HELIX". I am stuck and unable to make progress despite consulting many articles posted by others.

Here is where I am
var xhr = new XMLHttpRequest(); xhr.open("GET", "https://api.twitch.tv/helix/streams?user_login=STREAMER", true); xhr.setRequestHeader('Client-ID', 'CLIENT ID'); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ var data = JSON.parse(xhr.responseText); if(data["stream"] == null){ $("#statut").html("Le stream est inactif") }else{ $("#statut").html("Le stream est actif") } } } xhr.send(); 


Thank you for your guidance... ;)

EDIT :: Correction of code tags (adding the LANGUAGE for syntax highlighting)

3 answers

jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Hello,
You're stuck.. what does that mean?
What exactly are you stuck on?
Have you checked the javascript console of your browser (for that I strongly recommend doing it in firefox) the result of your ajax call?
It would also be good to add some console.log in your code to see what your different variables are worth...
And of course.. have you replaced the CLIENT-ID with your identifier (which you need to get from the api site)

I also note that you seem to be using Jquery on your page..... it would therefore be simpler to use the ajax function of jquery instead of making XMLHttpRequest..

--
Best regards,
Jordane
0
ThommyGames06 Posted messages 5 Status Member
 
Thank you for getting back to me, Jordane,

When I say "I'm stuck", I mean that my code isn't working. I've followed your advice and here's where I am:

 $.ajax({ url: "https://api.twitch.tv/helix/streams?user_login=STREAMER", dataType: 'json', headers: { 'Client-ID': 'My Client ID' }, success: function(data){ console.log(data) if (data["stream"] == null) { $("#info").html("The stream is inactive"); console.log(data["stream"]); } else { $("#info").html("The stream is active"); console.log(data["stream"]); } } }); 


The console returns the error 401 which is:
{error: "Unauthorized", status: 401, message: "OAuth token is missing"}


What does this mean?

Thank you,
Thommy ;)
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
This means that you did not send the authentication token
 headers: { $.ajax({ url: "https://api.twitch.tv/helix/streams?user_login=STREAMER", dataType: 'json', 'Client-ID': '<your client id>', 'Authorization': 'OAuth <your oauth token with channel_read scope>', success: function(data){ console.log(data) if (data["stream"] == null) { $("#info").html("The stream is inactive"); console.log(data["stream"]); } else { $("#info").html("The stream is active"); console.log(data["stream"]); } } }); 

Documentation to read for reference:
https://dev.twitch.tv/docs/authentication/getting-tokens-oauth
0
ThommyGames06 Posted messages 5 Status Member
 
I sent a request to get my token (thanks to the documentation) but it returns nothing (sorry for being so novice in the world of APIs).

 var xhr = new XMLHttpRequest(); xhr.open("POST", "https://id.twitch.tv/oauth2/token?client_id=MY CLIENT ID&client_secret=MY CLIENT SECRET&grant_type=client_credentials", true); console.log(xhr.responseText); xhr.send; $.ajax({ url: "https://api.twitch.tv/helix/streams?user_login=STREAMER", dataType: 'json', headers: { 'Client-ID': 'CLIENT ID', 'Authorization': 'OAuth TOKEN'}, success: function(data){ console.log(data) if (data["stream"] == null) { $("#info").html("The stream is inactive"); console.log(data["stream"]); } else { $("#info").html("The stream is active"); console.log(data["stream"]); } } }); 


P.S: I obtained the "client secret" on my Twitch application console by clicking on new secret.

Thanks again,
Thommy ;)
0
jordane45 Posted messages 30426 Registration date   Status Moderator Last intervention   4 830
 
Once you have obtained the token, you need to use it in your scripts
by replacing the 'TOKEN' in the text 'OAuth TOKEN' with .. well.. your token.
0