Implement OAuth on twitch team website

Hello, I have a website for a Twitch team, which worked before Twitch implemented and required the OAuth.

Before the change, my site could:

  • Display a random stream on a featured player selected from a users list.
  • Display “cards” with info about other online streamers from the team, which could be clicked on to change the featured stream.

I added the streamers by hand in an array.

My main code is this:

<?php

echo "<ul class='canales-live'>";

$channels = array('kozmica','Atsumu','katykabooom','Shintaragi','IlianaGatto','Nhurart','Popotrix','Codim','Cuyba','sandralfs','dandlee','Teliwiis') ;
$callAPI = implode(",",$channels);
$dataArray = json_decode(@file_get_contents('https://api.twitch.tv/kraken/streams?channel=' . $callAPI), true);


foreach($dataArray['streams'] as $mydata)
{
	
	if($mydata['_id'] != null)
	{
		$name 		= $mydata['channel']['name'];
		$game		= $mydata['channel']['game'];
		$status		= $mydata['channel']['status'];
		$url		= $mydata['channel']['url'];
		$avatar		= $mydata['channel']['logo'];
		$followers	= $mydata['channel']['followers'];
		$viewers	= $mydata['viewers'];
	$a++;
	echo "<li class='". $name . "'>
	<div class='img'><img src='";
	echo $avatar ."'/></div><div class='info'>";
	echo "<span class='streamer'> ". $name ."</span><br>";
	echo "<div class='titlegame'><span class='status'>". $status ."</span></BR>";
	echo "<em>". $game ."</em></div></BR>";
	echo "<span class='followers'><i class='fa fa-heart'></i> ". $followers ." seguidores</span></BR>";
	echo "<span class='viewers'><i class='fa fa-eye'></i> ". $viewers ." espectadores</span></BR>";
	//clearfix y cerrar li
	echo "</div><div class='clear'></div></li>"; 
	
	
	$estrimmmers[$a]= $mydata['channel']['name'];
	
	}
	else{
		$a=0;
	}
	
}

$rand_keys = rand($a, 1);

//echo $rand_keys;
 //echo str_replace('streamerscreativos', $estrimmmers[$rand_keys], ob_get_clean());
 
 
 if($a > 0){
	 echo str_replace('streamerscreativos', $estrimmmers[$rand_keys], ob_get_clean());
	 
 }
 else{
	 
	 echo '';
 }

//unset($channels);

echo "</ul>";
?>

I have been reading the API documentation and some githubs but I’m a designer and don’t fully understand how I can implement the OAuth in my existing code. I have been trying to get it to work copying and pasting bits from here and there with no success.

I have already registered my app and have a Client ID and Secret.

Help will be much appreciated since I have been breaking my head for a while now.

Thank you!

I’m not sure you mean oauth, but rather just lack a clientID on your API query.

You can add the clientID in the URL

$dataArray = json_decode(@file_get_contents(‘https://api.twitch.tv/kraken/streams?client_id=xxxxxxxxxxxxxxxxxxxxxxxxx&channel=’ . $callAPI), true);

But it is preferred to make the request via headers which can be accomplished via cURL

/* file_get_contents replaced with curl function */
function file_get_contents_curl($url) {
$clientID = array(
“Client-ID: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $clientID);
$data = curl_exec($ch);
curl_close($ch);
return $data;

}

Using this you could put in your clientID once and call file_get_contents_curl to replace any instance of file_get_contents

2 Likes

It worked!

First tried only adding the Client ID to the URL in the API query, it didn’t work, but then added the request via the code you posted in a php tag in the header, changed the file_get_contents_curl, like you said, and worked like a charm.

It was really simple but some how reading the docs it was not clear to me what I had to do.

Thank you so much Matt!

I was bored and finished something I wanted to do for a while, I think you might find a use for it. This will basically make a player like on the twitch team page, by grabbing the list of team members, finding the ones that are live and then selecting the one with the most viewers to put into the player, if none are live it will put the most viewed stream on twitch at the moment in the player. This can of course be tweaked for a specific game or another team.

see code at: http://isso.pro/twitchtest/teams.php.txt

2 Likes

Awesome! I’ll check it out.

Is there a way to set a random stream on the featured player instead of the one with the most viewers with that code? I run a team for creative streamers (in spanish) and want to help smaller ones to get a chance to be discovered.

I’ll see if I can take the part where it fetches the team members automatically and get it into my code, since I do have some little things in place.

Thank you so much, again.

Check back in a few hours, I’m about to go to sleep, but I’ll make it able to grab a random live team member tomorrow and post the changes.

Couldn’t sleep, This will select a random live team member each time the page is loaded.

http://isso.pro/twitchtest/kozmica.php.txt

1 Like

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