Php Unauthorized trying to access streams endpoint


I am using PHP to try to see if a streamer is live on the page load. I am using an APP key to do this.
Here is the code:


$clientID = "client"; // from the dashboard
$secret = "secret"; // from the dashboard
$twitchName = "Keegers02";

// See if the streamer is live... I hope :)
$opts =  array(
	'https' => array(
		'method' => "GET",
		'header' => "Authorization: Bearer $secret\r\n" .
					"Client-Id: $clientID"
	'http' => array(
		'method' => "GET",
		'header' => "Authorization: Bearer $secret\r\n" .
					"Client-Id: $clientID"

$context = stream_context_create($opts);

$getStreamersRaw = file_get_contents("$twitchName", false, $context); 

When running this locally I get 401 unauthorized (yes on the dashboard I have added http://localhost to the list of Redirect URIs). Remotely I get absolutely nothing, so I am more trusting the local process right now.

I have tried multiple versions of the code above (including, as you can see breaking apart http and https), but the results are the same.

Any thoughts as to what I am missing?

Thank you,

Your secret is not a token it is a secret

You need to first use your client ID and secret to generate an app access/client credentials token

Docs: Getting OAuth Access Tokens | Twitch Developers
An Example:

I ran the command given here: Twitch API Concepts | Twitch Developers
for Windows and got the access token. I replaced the secret with that token, and I am still getting Unauthorized.

You referred to it as

$secret = “secret”; // from the dashboard

So I assumed you did the secret from the dashboard.

Whats the body message of the response.
The HTTP Code is half the information

You might want to convert your code from file_get_contents to curl like my linked example, most hosts block first_get_contents from network operations.

I did, but I changed it per your instructions.

Completely empty.

Is Twitch doing that? I am still testing on a local machine.

No a hosting provider will not the api provdered being called

thats concerning you should get a descriptive json blob.
but having never used file_get_contents to make http requests I dont know what happens on http error

Best advice is to convert your code to curl it’ll be easier to work with

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