I was trying to check if a twitch streamer is live or not but when I tried it gave me a error.
here is my code:
async function isStreamerLive(username) {
const theUrl = `https://api.twitch.tv/helix/streams?user_login=${username}`
const headers = {
"Client-Id": 'ID', //hidden
"Authorization": 'TOKEN', //hidden
};
const response = await fetch(theUrl, headers);
const data = await response.json();
console.log(data)
return data?.data?.find(s => s.user_login === username.toLocaleLowerCase())
}
let username = 'kaicenat'
isStreamerLive(username)
Output:
{
error: 'Unauthorized',
status: 401,
message: 'OAuth token is missing'
}
Why is it saying OAuth token is missing? I am also providing client id and token when I tried code.
I dont know if its still relavent but you can check if a streamer is live by using
async function checkIfLive(username)
{
try
{
const response = await fetch(`https://twitch.tv/${username}`);
const sourceCode = await response.text();
if (sourceCode.includes("isLiveBroadcast"))
{
console.log(`${username} is live`);
}
else
{
console.log(`${username} is not live`);
}
}
catch (error)
{
console.log("Error occurred:", error);
}
}
let username = "kaicenat";
checkIfLive(username);
ill explain a bit more, basically when a streamer is live "isLiveBroadcast" is in the source code of the twitch page, but when the user isn't live it isnt there.