Twitch’s new Helix API requires a Twitch Auth token in order to make any sort of requests. Here we are going to show you how to get a Twitch Auth token in a variety of common programming languages.
Get Twitch Auth Token in jQuery
$.ajax({
url: 'https://id.twitch.tv/oauth2/token?client_id=CLIENTIDGOESHERE&client_secret=CLIENTSECRETGOESHERE&grant_type=client_credentials',
type: 'POST',
success: function(data) {
console.log(data)
console.log(data.access_token)
console.log(data.expires_in)
},
error: function(data) {
console.log('something went wrong! check your Client ID and Client Secret are setup correctly!')
}
})
Get Twitch Auth Token in Javascript
xhr = new XMLHttpRequest();
xhr.open("POST", 'https://id.twitch.tv/oauth2/token?client_id=CLIENTIDGOESHERE&client_secret=CLIENTSECRETGOESHERE&grant_type=client_credentials
');
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var data = xhr.responseText;
console.log(data)
console.log(data.access_token)
console.log(data.expires_in)
} else {
console.log('something went wrong! check your Client ID and Client Secret are setup correctly!')
}
}
xhr.send();
Get Twitch Auth Token in PHP
$ch = curl_init("https://id.twitch.tv/oauth2/token?client_id=CLIENTIDGOESHERE&client_secret=CLIENTSECRETGOESHERE&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
));
$response = curl_exec($ch);
$data = json_decode($response, true);
print_r($data)
echo $data['access_token']
echo $data['expires_in']