twitch get clips read api not working in php for wordpress

32 Views Asked by At
function display_twitch_videos($atts) {
    // Extract shortcode attributes if needed

    // Retrieve your client ID and client secret from your Twitch app
    $clientID = 'xxxxxxxxx';
    $clientSecret = 'xxxxxxxxx';

    // Twitch API endpoint for obtaining an access token using client credentials
    $tokenUrl = 'https://id.twitch.tv/oauth2/token';

    // Parameters for the token request
    $params = [
        'client_id' => $clientID,
        'client_secret' => $clientSecret,
        'grant_type' => 'client_credentials',
        'scope' => 'clips:read',
    ];

    // Initialize cURL session for the token request
    $ch = curl_init($tokenUrl);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Execute the token request
    $response = curl_exec($ch);

    // Check for cURL errors
    if (curl_errno($ch)) {
        return 'cURL error: ' . curl_error($ch);
    }

    // Check HTTP response code
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if ($httpCode !== 200) {
        return "Failed to obtain access token. HTTP response code: $httpCode. Response: $response";
    }

    curl_close($ch);

    // Decode the JSON response
    $tokenData = json_decode($response, true);

    // Check if access token is obtained successfully
    if (isset($tokenData['access_token'])) {
        // Use the access token in your API requests
        $accessToken = $tokenData['access_token'];

        // Use the obtained access token to make the actual API request
        $broadcasterUserId = 'xxxxxxx'; // Replace with the broadcaster's user ID
        $videoClipsUrl = "https://api.twitch.tv/helix/clips?broadcaster_id=$broadcasterUserId";

        $headers = [
            'Authorization: Bearer ' . $accessToken,
            'Client-Id: ' . $clientID,
        ];

        // Initialize cURL session for the API request
        $ch = curl_init($videoClipsUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // Execute the API request
        $response = curl_exec($ch);

        // Check for cURL errors
        if (curl_errno($ch)) {
            return 'cURL error: ' . curl_error($ch);
        }

        // Check HTTP response code
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if ($httpCode !== 200) {
            return "Failed to retrieve video clips. HTTP response code: $httpCode. Response: $response";
        }

        curl_close($ch);

        // Decode the JSON response for video clips
        $videoClipsData = json_decode($response, true);

        // Output the result
        ob_start(); // Start output buffering
        print_r($videoClipsData); // You can customize this output
        $output = ob_get_clean(); // Get the buffered content

        return $output;
    } else {
        // Handle the case where the access token is not obtained successfully
        return 'Failed to obtain access token. Twitch API response: ' . print_r($tokenData, true);
    }
}

add_shortcode('twitch_videos', 'display_twitch_videos');

i run this code for wordpress. basicly just need the clips to show on wordpress. but instead i get the error code: HTTP response code: 400. Response: {“status”:400,”message”:”invalid scope requested: ‘clips:read’”} for this code. im not sure where im doing things wrong here.

if anyone have suggestion please feel free to respond. i cant seem to find any solutions online either

i tried mostly what i could find, yes i did generate the code with help of chatgpt. i dont know if the scope read is changed for twitch but i couldnt find it there aswell.

0

There are 0 best solutions below