With these new changes to API, how exactly this works

On my Wordpress site I want to check if specific user is live and if it is, get that streamer public info (Steam thumb, title etc) . With this new token requirement I am running into “OAuth token is missing” error.

As I understand, first I need to get the token. Here is how I get token…

			$clientId = '123';
			$secret = '456';
			$user = 'anomaly';
			$ch = curl_init(); 
			$url = "https://id.twitch.tv/oauth2/token?client_id=$clientId&client_secret=$secret&grant_type=client_credentials";
			curl_setopt($ch, CURLOPT_URL, $url); 
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			$token = curl_exec($ch);

It will return me something like this…

{"access_token":"REMOVED","expires_in":4845352,"token_type":"bearer"}

Next I am going to make request to URL that is supposed to return me the data.

			$url = "https://api.twitch.tv/helix/streams?user_login=$user";
			$header = [
				"Client-ID: $clientId",
				"Authorization: Bearer $token->access_token",
			];
			curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
			$data = curl_exec($ch);
			curl_close($ch);

If I now var_dump out the $data I will get…

{"error":"Unauthorized","status":401,"message":"OAuth token is missing"}"

Removed your leaked access_token

$result = curl_exec($ch);

$token = json_decode($result);

Looks like you didn’t JSON Decode the token response before trying to use the token

1 Like

Thanks! Yes it works. I was so close. I earlier used json_decode, but removed and did not put it back -.-

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