Redirect URI/Login Authentication

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]

I got the file code from Matthew in this other post I made where he linked http://twitch.apiexampl.es/

Looks like you used most of my example from http://twitch.apiexampl.es/auth.php.txt
So next step is to check Your Server log for errors.

It’s possible that your call to https://api.twitch.tv/kraken/user is hanging because your host doesn’t allow out bound cURL connections OR you have a SSL negotiation problem

1 Like

Thank you…you just solved my problem…I forgot to set up SSL on my webserver

In your auth.php, I would suggest on line 7 in your if statement using:

if(isset($_GET['code']))

instead of:

if($_GET['code'])

because it will throw:

Notice: Undefined index: code in [path/to/php/file] on line [num]

@BarryCarlyon

Naturally, the example is a quick and dirty example, and is not even the code I actually use on production.

Further more in Production, server notices should not be shown.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.