Getting unauthorized error when using Twitch API access token

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?

sounds like ur storing the refresh token instead of access token maybe? those twitch access tokens expire pretty quick, so u gotta use the refresh token to get new ones. check what exactly ur saving to the db.