[Lua] How to get follower count

I’m new to the Twitch API, so I don’t know much about how it works. I was wanting to use the API to show my follower count inside of a Roblox game. I have the Client ID and Client Secret set up, I just don’t know how to use the API in Lua.

If anyone understands the Lua API service and can help me out, that would be great!

That would be this endpoint: Reference | Twitch Developers

For querying this endpoint you need a “User Access Token”. All about that is here: Authentication | Twitch Developers

I think for your case is this the best way to go: Getting OAuth Access Tokens | Twitch Developers

This is incorrect information

If you call it with client credentials or a user token without the scope then JUST the follower count is returned, which is all OP needs.

A user token is not required.

As to OP’s question:

You just need to be able to make HTTP requests.

But I don’t know if LUA is crippled inside Roblox to allow or prevent making HTTP requests.

Which makes this a roblox issue primarily.

The following might help

I know that I have to use HTTP to receive the information, but I mostly don’t know what HTTP Url to use. I’ve tried using a few different Urls, and either seen “Page not found” or “Oauth token needed”.

Is there a specific HTTP Url that can be used without needing an Oauth token?

An oAuth token is required

So you’d first do HTTP calls to get a token.
Then use that token to get your follower count in a follow up HTTP Call.

I’ve tried that and received an error saying “Unrecognized request option url”. I’ll provide the code I used.

local http = game:GetService("HttpService")
local client_id = "<your_client_id>" -- Replace <your_client_id> with your Twitch API client ID
local client_secret = "<your_client_secret>" -- Replace <your_client_secret> with your Twitch API client ID
local ClientAccountName = "<your_channel_name>"

local function GetToken()

	-- Make an HTTP request to the Twitch API to get an access token
	local url1 = "https://id.twitch.tv/oauth2/token"
	local data = "client_id="..client_id.."&client_secret="..client_secret.."&grant_type=client_credentials"
	local headers = {
		["Content-Type"] = "application/x-www-form-urlencoded",
		["Content-Length"] = #data,
	}
	local response, status, headers = http:RequestAsync({
		url = url1.."&"..data,
		method = "POST",
		headers = headers,
	})

	-- Parse the response to extract the access token
	if status == 200 then
		local data = http:JSONDecode(response)
		local access_token = data.access_token
		print(access_token)
		return access_token
	else
		print("Error getting access token: "..status)
		return false
	end
end

-- Make an HTTP request to the Twitch API to get the user's follower count
local url = "https://api.twitch.tv/helix/users/follows?to_id="..ClientAccountName
local headers = {
	["Client-ID"] = client_id,
	["Authorization"] = "Bearer "..GetToken(), 
}

local response, status, headers = http:RequestAsync({
	url = url,
	headers = headers,
})

-- Parse the response to extract the follower countc
if status == 200 then
	local data = http:JSONDecode(response)
	local follower_count = data.total_followers
	print("Follower count for "..ClientAccountName..": "..follower_count)
else
	print("Error getting follower count: "..status)
end

From the limited reading I’ve done

Roblox LUA can’t make HTTP POST requests.

I did some searching, and the way I tried making an HTTP POST request was wrong. It can be done from this code.

game:GetService("HttpService"):PostAsync("<url>","<data>","<content_type>","<compress>")

I tried entering the same info into the new HTTP POST code and received “HTTP 404 (Not Found)”

Then it didn’t call the URL you thought it called I guess.

Debug it and see if you can get it to tell you what URL it called?

1 Like

This is the code I used. It should have called the URL “https://id.twitch.tv/oauth2/token

local http = game:GetService("HttpService")
local client_id = "<your_client_id>" -- Replace <your_client_id> with your Twitch API client ID
local client_secret = "<your_client_secret>" -- Replace <your_client_secret> with your Twitch API client ID
local ClientAccountName = "<your_channel_name>"


-- Make an HTTP request to the Twitch API to get an access token
local url = "https://id.twitch.tv/oauth2/token"
local data = "client_id="..client_id.."&client_secret="..client_secret.."&grant_type=client_credentials"

local request = http:PostAsync(url,data,Enum.HttpContentType.ApplicationUrlEncoded,false)
local data = http:JSONDecode(request)
print(data.access_token)

I do not see anything wrong with the Twitch parts of this.

What you’ve input looks correct/matches Getting OAuth Access Tokens | Twitch Developers

1 Like

I did some debugging, changed some values that the code was sending, and got it to get an OAuth token. I updated the first script from before to use the new code. Now it errors saying “HTTP 400 (Bad Request)” on line 25. I’m guessing that means the URL is wrong.

local http = game:GetService("HttpService")
local client_id = "<your_client_id>" -- Replace <your_client_id> with your Twitch API client ID
local client_secret = "<your_client_secret>" -- Replace <your_client_secret> with your Twitch API client ID
local ClientAccountName = "<your_channel_name>"

local function GetToken()

	-- Make an HTTP request to the Twitch API to get an access token
	local url = "https://id.twitch.tv/oauth2/token"
	local data = "client_id="..client_id.."&client_secret="..client_secret.."&grant_type=client_credentials"

	local request = http:PostAsync(url,data,Enum.HttpContentType.ApplicationUrlEncoded,false)
	local data = http:JSONDecode(request)
	print(data.access_token)
	return data.access_token
end

-- Make an HTTP request to the Twitch API to get the user's follower count
local url = "https://api.twitch.tv/helix/users/follows?to_id="..ClientAccountName
local headers = {
	["Client-ID"] = client_id,
	["Authorization"] = "Bearer "..GetToken(), 
}

local response = http:GetAsync(url,false,headers)
local data = http:JSONDecode(response)
local follower_count = data.total_followers
print("Follower count for "..ClientAccountName..": "..follower_count)

Most Twitch API’s requires a USERID not a USERNAME

So I suspect you fed it your username instead of your user ID

Use Get users to convert from a name to an ID

1 Like

I replaced the username with the userid, and it successfully got my follower count. Thanks for the help! I’ll keep the code here in case anyone finds it useful.

local http = game:GetService("HttpService")
local client_id = "<your_client_id>" -- Replace <your_client_id> with your Twitch API client ID
local client_secret = "<your_client_secret>" -- Replace <your_client_secret> with your Twitch API client ID
local ClientAccountName = "<your_client_name>"

local function GetToken()

	-- Make an HTTP request to the Twitch API to get an access token
	local url = "https://id.twitch.tv/oauth2/token"
	local data = "client_id="..client_id.."&client_secret="..client_secret.."&grant_type=client_credentials"

	local request = http:PostAsync(url,data,Enum.HttpContentType.ApplicationUrlEncoded,false)
	local data = http:JSONDecode(request)
	print(data.access_token)
	return data.access_token
end

-- Make an HTTP request to the Twitch API to get the user's follower count
local url = "https://api.twitch.tv/helix/users/follows?to_id=<your_client_id>" -- Replace <your_client_id> with your Twitch ID
local headers = {
	["Client-ID"] = client_id,
	["Authorization"] = "Bearer "..GetToken(), 
}

local response = http:GetAsync(url,false,headers)
local data = http:JSONDecode(response)
local follower_count = data.total
print("Follower count for "..ClientAccountName..": "..follower_count)

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