Confused on this oauth stuff

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

}