I’m currently facing challenges while trying to implement Twitch OAuth for user login in my web application. I adapted some example code I found online, but it seems something isn’t working correctly.
The issues appear to arise around the section checking the HTTP response with if ($response['http_code'] == 200). I’m not completely sure what’s wrong. Here’s the part that’s causing confusion:
if ($response['http_code'] == 200) {
$token_data = json_decode($response_body, true);
// retrieve user info
$user_curl = curl_init('https://api.twitch.tv/kraken/user');
curl_setopt($user_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($user_curl, CURLOPT_HTTPHEADER, array(
'Accept: application/vnd.twitchtv.v3+json',
'Client-ID: ' . $app_id,
'Authorization: OAuth ' . $token_data['access_token']
));
$user_response = curl_exec($user_curl);
$user_info = curl_getinfo($user_curl);
curl_close($user_curl);
if ($user_info['http_code'] == 200) {
$user_data = json_decode($user_response);
echo '<p>Welcome back ' . $user_data->display_name . '!</p>';
// USER AUTHENTICATED SUCCESSFULLY
} else {
echo '<p>Something went wrong, please try again</p>';
}
}
Here’s my entire code:
$app_id = 'YourAppID';
$app_secret = 'YourAppSecret';
$callback_url = 'http://yoursite.com/callback';
if ($_GET['code']) {
$oauth_url = 'https://api.twitch.tv/kraken/oauth2/token';
$post_data = array(
'client_id' => $app_id,
'client_secret' => $app_secret,
'grant_type' => 'authorization_code',
'redirect_uri' => $callback_url,
'code' => $_GET['code']
);
$ch = curl_init($oauth_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response_body = curl_exec($ch);
$response = curl_getinfo($ch);
curl_close($ch);
if ($response['http_code'] == 200) {
$token_data = json_decode($response_body, true);
$user_curl = curl_init('https://api.twitch.tv/kraken/user');
curl_setopt($user_curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($user_curl, CURLOPT_HTTPHEADER, array(
'Accept: application/vnd.twitchtv.v3+json',
'Client-ID: ' . $app_id,
'Authorization: OAuth ' . $token_data['access_token']
));
$user_response = curl_exec($user_curl);
$user_info = curl_getinfo($user_curl);
curl_close($user_curl);
if ($user_info['http_code'] == 200) {
$user_data = json_decode($user_response);
echo '<p>Welcome back ' . $user_data->display_name . '!</p>';
} else {
echo '<p>Something went wrong, please try again</p>';
}
} else {
echo '<p>Authentication failed, please try again</p>';
}
} else {
$permissions = array(
'user_read' => 1,
);
$scope_string = '';
foreach ($permissions as $permission => $enabled) {
if ($enabled) {
$scope_string .= $permission . '+';
}
}
$scope_string = rtrim($scope_string, '+');
$login_url = 'https://api.twitch.tv/kraken/oauth2/authorize?response_type=code';
$login_url .= '&client_id=' . $app_id;
$login_url .= '&redirect_uri=' . $callback_url;
$login_url .= '&scope=' . $scope_string;
$login_url .= '&force_verify=true';
echo '<a href="' . $login_url . '">Click here to login with Twitch</a>';
}
Can anyone assist me in identifying the issue? I’m still learning PHP and the Twitch API documentation can be quite confusing.