Twitch api cURL php return Invalid OAuth token error

Hi, can someone help me to fix this issue, I spend hours trying to fix the problem, here is my code snippet:

$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, "");//Endpoint
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array(
		'Content-type: application/json',
		'Authorization: Bearer <Client Secret>',
		'Client-ID: <Client ID>'
	$profile_data = json_decode(curl_exec($ch), true);
	echo curl_error($ch);

	if (!isset($profile_data['data'][0])) {
		echo 'Users channel is Offline';
	} else {
		echo 'Users channel is LIVE!';

	echo "<pre>";
	echo "</pre>";

I get this error:

    [error] => Unauthorized
    [status] => 401
    [message] => Invalid OAuth token

I think i need to get 0auth token, but i don’t know how?? Can someone send me direct link where I can get it or correct my code is it’s wrong.

Thanks in advance.

You need to use a token. Not a Client Secret.

For the code you are presented a Client Credentials/App Access Token Suffices

Here is a PHP code example for token generation

Thanks for reply.

  1. Do I need to upload the github files in my cPanel?
  2. Regarding the last step, how I can do this : In a console/terminal, run php and the script you want to test?

No, modify your existing script as needed. The GitHub is an example that demonstrates how to get a token.

As an example this can example be run as a command line script.

If you don’t have a command line/ssh on your hosting then you don’t have a command line to use. So your current script is running as a web page I guess, so you can put the code there inside your webpage

Got it, I have uploaded the config.php and generate_and_maintain_token.php, and I edited the config with the app client id and secret, but when trying to access my localhost http://localhost/twitch/generate_and_maintain_token.php to generate token it give me that error:

Failed with 0

See the screenshot, I tried also with terminal.
generate token error

How to fix it please?

Failed with 0 doesn’t offer any information as to what the issue is.

In most cases, as this is locahost, this suggests a problem with SSL/TLS negotiation most likely that your local SSL/TLS cert bundle is invalid/out of date and needs to be updated

Refer to the instructions for this for whatever is running/providing your localhost

You’re right, I tried to upload it to live server cPanel and it works fine… thanks so much, do i need to generate this token every time when it expire expires_in, how to make it automatically with cron job maybe?

Offers an opinion on a “cron job”

In this example it sores the generated token in auth.json

And whenever generate_and_maintain_token.php is run (likely via cron) it will load the token, validate it and regenerate if theres less than an hour left on it.

So, if you retrain the token into “storage” then you can use the token till it’s close to expire.
But depends how you use the otken and what makes sense for your application and usage

Ok, thank you.

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