Check stream live is online or not

Hello good, I have this code that is to check if the streamer is online or not, but it always returns false, can you help me please.

 static HttpClientHandler hcHandle = new HttpClientHandler();

    static async Task<bool> IsOnline(string channel)
    {
        using (var hc = new HttpClient(hcHandle, false))
        // false here prevents disposing the handler, which should live for the duration of the program and be shared by all requests that use the same handler properties
        {
            hc.DefaultRequestHeaders.Add("Client-ID", "dagsugbfi4u9ytbq6cn2kj681qvtzu");
            //hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", "your oauth token, should you have one (you should, but not required)");
            //hc.DefaultRequestHeaders.UserAgent.ParseAdd("this would be good practice to set to your own");
            hc.Timeout = TimeSpan.FromSeconds(15); // good idea to set to something reasonable

            using (var response = await hc.GetAsync($"https://api.twitch.tv/helix/streams?user_login={channel}"))
            {
                response.EnsureSuccessStatusCode(); // throws, if fails, can check response.StatusCode yourself if you prefer
                string jsonString = await response.Content.ReadAsStringAsync();
                // TODO: parse json and return true, if the returned array contains the stream


            }
        }
        return false;
    }




 public async void button2_Click(object sender, EventArgs e)
    {
        bool is_valid = await IsOnline("thead");

        if(is_valid)
        {
            this.Alert("thead is online", FormCoscu.enmType.coscu);

        }
        else
        {
            MessageBox.Show("stream is offline");

        }
    }

Are you getting a 200 OK from the API?

Whats the channel you are calling? It needs/should be all lowercase/the login username of the user. Not the localized display name

Is this the actual code you’re using? Because it’s incomplete and even has a commented TODO section that the actual logic of checking if the stream exists or not still has to be written, so if you haven’t finished writing the code then of course it would return false.

If it’s not the actual code you’re using, could you please show us what the actual request you’re making is.

If the code is not finished and I don’t know how to finish it, if you could help me, I am new with this api

Im as well receiving the 200 OK error. My response: {data:[],pagination:{}}

local Data = {
Url = “https://api.twitch.tv/helix/streams?user_login=12gaugenick
Method = “GET”,
Headers = {
[“Client-ID”] = “p”,
},
}
local Request = Http:RequestAsync(Data)
print(Request.StatusCode, Request.StatusMessage, Request.Body)
return false

You’ve jumped on someone elses thread.

Whats the issue?

Other than your ClientID being invalid, and getting nothing in data which would indicate that 12gaugenick is offline?

I removed my client id in my former comment, although the one in my actual code is valid. So no data = offline?

ClientID’s are public data and don’t need to be censored.
Client Secrets, on the other hand, do.

No data = no online streams for the users requested, that is correct

So it roughly takes 34 seconds to update. Thank you for clarifying some things! Hopefully this will also help @PEC_UHC

Cache times can vary.

if you want to be up to date for when the API changes, you should consider webhooks instead

https://dev.twitch.tv/docs/api/webhooks-guide

For my thing, I dont believe a webhook is necessary. Although I will post the finished code for anyone who would like it. (Lua)

local CheckIfOnline = function()
local Data = {
Url = “https://api.twitch.tv/helix/streams?user_login=12gaugenick”,
Method = “GET”,
Headers = {
[“Client-ID”] = “p68bczp3bnsopaf6378auzjk4nu8wl&quot”
},
}
local Request = Http:RequestAsync(Data)
if Request.StatusCode == 200 then
local TwitchData = Http:JSONDecode(Request.Body).data[1]
if TwitchData.type == “live” then
return {
Viewers = TwitchData.viewer_count,
Title = TwitchData.title,
IsLive = TwitchData.type
}
else
return false
end
else
warn(“API Error”)
end
return false
end

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