if(isset($_GET['post'])) {
define("CLIENT_ID", "xxx");
define("CLIENT_SECRET", "xxx");
$keys = false;
if (file_exists(__DIR__ . '/auth.json')) {
$keys = json_decode(file_get_contents(__DIR__ . '/auth.json'));
}
$generate_token = true;
if ($keys) {
// validate the token
$ch99 = curl_init('https://id.twitch.tv/oauth2/validate');
curl_setopt($ch99, CURLOPT_HTTPHEADER, array(
'Authorization: OAuth ' . $keys->access_token
));
curl_setopt($ch99, CURLOPT_RETURNTRANSFER, true);
$r = curl_exec($ch99);
$i = curl_getinfo($ch99);
curl_close($ch);
if ($i['http_code'] == 200) {
// the token appears good
$generate_token = false;
// optional to check the expires
$token = json_decode($r);
if (json_last_error() == JSON_ERROR_NONE) {
if ($token->expires_in < 3600) {
// less than an hour left
// make a new token
echo 'Token close to expire. Regenerate';
$generate_token = true;
}
} else {
echo 'Failed to parse JSON. Assume dead token';
$generate_token = true;
}
}
}
if ($generate_token) {
$ch99 = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($ch99, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch99, CURLOPT_POST, true);
curl_setopt($ch99, CURLOPT_POSTFIELDS, array(
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'grant_type' => "client_credentials"
));
$r = curl_exec($ch99);
$i = curl_getinfo($ch99);
curl_close($ch99);
if ($i['http_code'] == 200) {
$token = json_decode($r);
if (json_last_error() == JSON_ERROR_NONE) {
echo 'Got token';
print_r($token);
// store the token for next run
file_put_contents(__DIR__ . '/auth.json', $r, JSON_PRETTY_PRINT);
} else {
echo 'Failed to parse JSON';
}
} else {
echo 'Failed with ' . $i['http_code'] . ' ' . $r;
}
} else {
echo 'Token OK'; echo "</br>"; echo "</br>";
print_r($keys);
}