Last active
July 16, 2024 11:12
-
-
Save Scampi-ml/9fe54f88ba4dcea7dadb37864e9a0d94 to your computer and use it in GitHub Desktop.
Get a twitch oauth token using client id & client secret
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TwitchTokenGenerator | |
{ | |
public $tw_auth_url; | |
public $tw_client_id; | |
public $tw_client_secret; | |
public function getTwitchAccessToken() | |
{ | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); | |
curl_setopt($ch, CURLOPT_URL, $this->tw_auth_url . 'token?client_id=' . $this->tw_client_id . '&client_secret=' . $this->tw_client_secret . '&grant_type=client_credentials'); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array | |
( | |
'Content-Type: application/json' | |
)); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
$data = json_decode($response, true); | |
//return $data['access_token']; | |
return $data; | |
} | |
public function time_format($seconds, $mode = "long", $extra = '') | |
{ | |
$names = array('long' => array("year", "month", "day", "hour", "minute", "second"), 'short' => array("yr", "mnth", "day", "hr", "min", "sec")); | |
$seconds = floor($seconds); | |
$minutes = intval($seconds / 60); | |
$seconds -= ($minutes * 60); | |
$hours = intval($minutes / 60); | |
$minutes -= ($hours * 60); | |
$days = intval($hours / 24); | |
$hours -= ($days * 24); | |
$months = intval($days / 31); | |
$days -= ($months * 31); | |
$years = intval($months / 12); | |
$months -= ($years * 12); | |
$result = array(); | |
if ($years) | |
$result[] = sprintf("%s%s %s%s", number_format($years), ' '.$extra, $names[$mode][0], $years == 1 ? "" : "s"); | |
if ($months) | |
$result[] = sprintf("%s%s %s%s", number_format($months), ' '.$extra, $names[$mode][1], $months == 1 ? "" : "s"); | |
if ($days) | |
$result[] = sprintf("%s%s %s%s", number_format($days), ' '.$extra, $names[$mode][2], $days == 1 ? "" : "s"); | |
if ($hours) | |
$result[] = sprintf("%s%s %s%s", number_format($hours), ' '.$extra, $names[$mode][3], $hours == 1 ? "" : "s"); | |
if ($minutes && count($result) < 2) | |
$result[] = sprintf("%s%s %s%s", number_format($minutes), ' '.$extra, $names[$mode][4], $minutes == 1 ? "" : "s"); | |
if (($seconds && count($result) < 2) || !count($result)) | |
$result[] = sprintf("%s%s %s%s", number_format($seconds), ' '.$extra, $names[$mode][5], $seconds == 1 ? "" : "s"); | |
return implode(", ", $result); | |
} | |
} | |
if( isset($_POST['tw_client_id']) && isset($_POST['tw_client_secret'])) | |
{ | |
$tcd = new TwitchTokenGenerator(); | |
$tcd->tw_auth_url = 'https://id.twitch.tv/oauth2/'; | |
$tcd->tw_client_id = htmlspecialchars($_POST['tw_client_id']); | |
$tcd->tw_client_secret = htmlspecialchars($_POST['tw_client_secret']); | |
$token = $tcd->getTwitchAccessToken(); | |
echo '<pre>'; | |
var_dump( $token ); | |
echo '</pre>'; | |
echo '<br>'; | |
echo 'Expires in: ' . $tcd->time_format($token['expires_in']); | |
echo '<br>'; | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>TwitchTokenGenerator</title> | |
</head> | |
<body> | |
<h2>TwitchTokenGenerator</h2> | |
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> | |
<label for="fname">tw_client_id:</label><br> | |
<input type="text" id="tw_client_id" name="tw_client_id" value="" size="50"><br> | |
<label for="lname">tw_client_secret:</label><br> | |
<input type="text" id="lname" name="tw_client_secret" value="" size="50"><br><br> | |
<input type="submit" value="Submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment