I’m trying to fetch Twitch clips in my WordPress site but I’m encountering a 400 error indicating an invalid scope. Here’s the code I’m using:
function fetch_twitch_clips($attributes) {
$appID = 'your_app_id';
$appSecret = 'your_app_secret';
$authEndpoint = 'https://id.twitch.tv/oauth2/token';
$authParams = [
'client_id' => $appID,
'client_secret' => $appSecret,
'grant_type' => 'client_credentials',
'scope' => 'clips:read',
];
$curl = curl_init($authEndpoint);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($authParams));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$authResponse = curl_exec($curl);
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($statusCode !== 200) {
return "Authentication failed. Status: $statusCode. Error: $authResponse";
}
curl_close($curl);
$authData = json_decode($authResponse, true);
if (isset($authData['access_token'])) {
$token = $authData['access_token'];
$channelId = 'broadcaster_id_here';
$clipsEndpoint = "https://api.twitch.tv/helix/clips?broadcaster_id=$channelId";
$requestHeaders = [
'Authorization: Bearer ' . $token,
'Client-Id: ' . $appID,
];
$apiCurl = curl_init($clipsEndpoint);
curl_setopt($apiCurl, CURLOPT_HTTPHEADER, $requestHeaders);
curl_setopt($apiCurl, CURLOPT_RETURNTRANSFER, true);
$clipResponse = curl_exec($apiCurl);
$responseCode = curl_getinfo($apiCurl, CURLINFO_HTTP_CODE);
curl_close($apiCurl);
if ($responseCode === 200) {
$clipsData = json_decode($clipResponse, true);
return print_r($clipsData, true);
}
}
return 'Token generation failed';
}
add_shortcode('twitch_clips', 'fetch_twitch_clips');
The error message says: {"status":400,"message":"invalid scope requested: 'clips:read'"}. I’m uncertain about what’s wrong with the scope parameter. Has Twitch modified their API scopes lately? Any guidance would be greatly appreciated.