Hi, I am new to using the TwitchAPI and I am trying to implement login authentication with twitch into my website, however when I run the script i get to the authorization part, then everything just freezes and the api.twitch.tv page just infinitely loads when trying to redirect back to my site, is this an apache setting? or am I doing something wrong?
EDIT #1: I have looked on the other forum posts, but everyone seems to have elevated knowledge of the API as I’m still trying to find good documentation on it.
EDIT #2: Also is it better to use the api.twitch.tv or the passport.twitch.tv for NodeJS, or is there a considerable difference?
[code] <?php
$client_id = ‘’;
$client_secret = ‘’;
$redirect_uri = ‘’;
$check = $_GET[‘code’];
if ($check != null) {
$token_url = ‘https://api.twitch.tv/kraken/oauth2/token’;
$data = array(
‘client_id’ => $client_id,
‘client_secret’ => $client_secret,
‘grant_type’ => ‘authorization_code’,
‘redirect_uri’ => $redirect_uri,
‘code’ => $_GET[‘code’]
);
$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
$result = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
$result = json_decode($result, true);
// get
$curl = curl_init('https://api.twitch.tv/kraken/user');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/vnd.twitchtv.v3+json',
'Client-ID: ' . $client_id,
'Authorization: OAuth ' . $result['access_token']
));
curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
$user = curl_exec($curl);
$i = curl_getinfo($curl);
curl_close($curl);
if ($i['http_code'] == 200) {
$user = json_decode($user);
echo '<p>Thanks ' . $user->display_name . ' <3</p>';
} else {
echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
}
} else {
echo '<p>An error occured, please <a href="/index.php">click here and try again</a></p>';
}
} else {
$scopes = array(
‘user_read’ => 1
);
$req_scope = '';
foreach ($scopes as $scope => $allow) {
if ($allow) {
$req_scope .= $scope . '+';
}
}
$req_scope = substr($req_scope, 0, -1);
$auth_url = 'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code';
$auth_url .= '&client_id=' . $client_id;
$auth_url .= '&redirect_uri=' . $redirect_uri;
$auth_url .= '&scope=' . $req_scope;
$auth_url .= '&force_verify=true';
echo '<a href="' . $auth_url . '">Please Click this Link to Authenticate with Twitch</a>';
}
[/code]