Confused on this oauth stuff

Hi, I currently have a real simple cron to query my own channel to see if I am live, and then get the game name. Now I got an email to say about authentication, which I really hope I don’t have to do to because it’s overkill and there’s no need for it.

Here is my code:

define("APP_ROOT", dirname( dirname(__FILE__) ) . '/public_html');

require APP_ROOT . "/includes/cron_bootstrap.php";

// grab details of their stream, will be blank of they're not live (API limits ~30 per minute)
$stream = core::file_get_contents_curl("https://api.twitch.tv/helix/streams?user_id=50905707", "GET", NULL, array("Client-ID: ". $core->config('twitch_dev_key')));
$stream_details = json_decode($stream, true);

// are they streaming a specific game?
if (isset($stream_details['data'][0]['game_id']))
{
	$game = core::file_get_contents_curl("https://api.twitch.tv/helix/games?id=".$stream_details['data'][0]['game_id'], "GET", NULL, array("Client-ID: ". $core->config('twitch_dev_key')));
	$game_details = json_decode($game, true);
	$game_name = $game_details['data'][0]['name'];

	$stream_details['game_name'] = $game_name; // add the game name to the details array
}

$to_file = json_encode($stream_details);

$fp = fopen(APP_ROOT . '/uploads/goltwitchcheck.json', 'w'); 
fwrite($fp, $to_file);
fclose($fp);
?>

Can someone advise me on how I can ensure such a simple thing can continue working?

Surely I don’t need a whole authentication flow, to query a channel is live and the game? We already have to register the app and get a client id/secret - there’s no need for authentication unless you’re editing something or getting private info…this is frustrating.

Use a server to server aka App access Token

1 Like

Interesting.

Okay, I think I have this working correctly. Please let me know if you see anything odd. It checks for a stored token and uses that, if none it grabs an application token. It then checks the auth, if return is empty then it grabs and stores a new token and then tries again.

function get_access_token($core)

{

$auth = core::file_get_contents_curl('https://id.twitch.tv/oauth2/token?client_id='.$core->config('twitch_dev_key').'&client_secret='.$core->config('twitch_secret').'&grant_type=client_credentials', "POST", NULL);

$auth_details = json_decode($auth, true);

// store the access token

$to_file = $auth_details['access_token'];

$fp = fopen(ACCESS_TOKEN_FILE, 'w');

fwrite($fp, $to_file);

fclose($fp);

return true;

}

// get a new access token if one doesn't exist

if (!file_exists(ACCESS_TOKEN_FILE))

{

echo 'Grabbing new access token - file doesn\'t exist';

get_access_token($core);

}

$access_token = file_get_contents(ACCESS_TOKEN_FILE);

function validate_token ($access_token)

{

$validate = core::file_get_contents_curl('https://id.twitch.tv/oauth2/validate', 'GET', NULL, array('Authorization: OAuth '.$access_token));

$validation_details = json_decode($validate, true);

// need to refresh the details

if (empty($validation_details))

{

if(get_access_token($core))

{

validate_token($access_token);

}

echo 'Grabbing new access token - invalid.';

return false;

}

return $validation_details;

}

$validation_details = validate_token($access_token);

// grab details of their stream, will be blank of they're not live)

$stream = core::file_get_contents_curl("https://api.twitch.tv/helix/streams?user_id=".CHANNEL_ID, "GET", NULL, array("Client-ID: ". $core->config('twitch_dev_key'), 'Authorization: OAuth '.$access_token));

$stream_details = json_decode($stream, true);

// are they streaming a specific game?

if (isset($stream_details['data'][0]['game_id']))

{

$game = core::file_get_contents_curl("https://api.twitch.tv/helix/games?id=".$stream_details['data'][0]['game_id'], "GET", NULL, array("Client-ID: ". $core->config('twitch_dev_key'), 'Authorization: OAuth '.$access_token));

$game_details = json_decode($game, true);

$game_name = $game_details['data'][0]['name'];

$stream_details['game_name'] = $game_name; // add the game name to the details array

}

Code flow seems ok

1 Like

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