Tryng to get php code for wordpress where i can get the twitch clips only

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');

unf i get error code : Failed to obtain access token. 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.

i changed the scope to edit. but now i get diff error = Array ( [data] => Array ( ) [pagination] => Array ( ) )


now i get this error on wordpress. i have fixed everything and it should show it but it doesnt. any solution?

Client Credential Tokens cannot have scopes.

There is also no clips:read scope - Twitch Access Token Scopes | Twitch Developers

It won’t do anything here due to client creds scopes do nothing on Twitch

This error suggests you embedded the clip in correctly.

Perhaps the parent is wrong.

im sorry if i sound far fetched but what does it mean with parent?

refer to parent in the table

foreach ($videoClipsData['data'] as $clip) {
                $embedCodes[] = '<iframe src="' . $clip['embed_url'] . '" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620" parent="dev-site.eldo.gg"></iframe>';
            }

considering i use https://dev-site.eldo.gg/?page_id=95 as my wordpress site. still get the same error aswell.

On the linked page there is no parent at all.

The correct Embed URL is src="https://clips.twitch.tv/embed?clip=FamousCuteFlamingoVoteYea-RIjYz2FX86Wp4TxV&parent=dev-site.eldo.gg

the thing is im tryng to set up atomated versjon where it collects all video clips of the channel. not just one, thats also one challenge im facing on this.

I’ve presented the relevant information needed to get one going.

So modify your code as needed to achieve what you want.

Is wrong as the parent is part of the URL not an iFrame parameter

foreach ($videoClipsData['data'] as $clip) {
                $embedCodes[] = '<iframe src="' . $clip['embed_url'] . '&parent=dev-site.eldo.gg" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="620"></iframe>';
            }

thank you very much for providing step by step help! and yes now it all works.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.