I’m working with Twitch API and running into authentication problems. When I try to fetch user data using my access token, I keep getting an unauthorized response.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/helix/users');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 25);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if($access_token) {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $access_token,
'Client-ID: ' . $client_id
]);
}
$response = curl_exec($ch);
curl_close($ch);
The API returns this error message:
{"error":"Unauthorized","status":401,"message":"Token invalid or missing required scope"}
What’s weird is that the token works fine right after I get it from the OAuth callback page. But once I store it in my database and try to use it later from a different page, it stops working. The token seems to become invalid somehow even though I’m storing it correctly. Has anyone faced this issue before?