2019-07-06
How to list all the videos posted by a youtube channel?
stackoverflow
Question

I'm making a website to accompany my youtube channel, and I want to have a list of all the latest videos posted on my channel on a page on the website, similar to this website: http://hermitcraft.com/ but only for one specific channel.

I'm new to the google apis system and find it thoroughly overwhelming.

My web sever is apache and runs php 7, but as it is a web host I do not have access to any console.

How do I approach solving this problem?

Answer
1

Assuming that you're able to program in PHP, I do suggest to start small and proceed incrementally from YouTube Data API Overview, PHP Quickstart and PHP Client Library: Getting Started. In any case, the reference documentation is your best friend -- only that you have to get acquainted with it.

You'll use the PHP client library code, thus clone that locally to your computer.

For the time being do not bother with OAuth authentication, only get from Google's developers console an API key for to use it with API's PlaylistItems endpoint, queried for the given channel's uploads list.

There is some sample code on Github for obtaining an user's uploads list, but that code is quite old and most likely has issues (it also uses OAuth authorization, which I already recommended you to not bother with). Here is the essential part of that code (I modified it a bit: replaced 'mine' => 'true' with 'id' => $YOUR_CHANNEL_ID; you have to test this code though):

  try {
    // Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'id' => $YOUR_CHANNEL_ID,
    ));

    $htmlBody = '';
    foreach ($channelsResponse['items'] as $channel) {
      // Extract the unique playlist ID that identifies the list of videos
      // uploaded to the channel, and then call the playlistItems.list method
      // to retrieve that list.
      $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];

      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $uploadsListId,
        'maxResults' => 50
      ));

      $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>";
      foreach ($playlistItemsResponse['items'] as $playlistItem) {
        $htmlBody .= sprintf('<li>%s (%s)</li>', $playlistItem['snippet']['title'],
          $playlistItem['snippet']['resourceId']['videoId']);
      }
      $htmlBody .= '</ul>';
    }
  } catch (Google_Service_Exception $e) {
    $htmlBody = sprintf('<p>A service error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  } catch (Google_Exception $e) {
    $htmlBody = sprintf('<p>An client error occurred: <code>%s</code></p>',
      htmlspecialchars($e->getMessage()));
  }

From the get go, you must be aware of the quota system the API is implementing. Depending on usage patterns, the quotas may put rather tight limits on the number of calls an user is allowed to make on various endpoints of the API. At any time, the Google's developers console is showing you your current quotas.

Finally, an useful tool for debugging your app: the APIs Explorer. It allows you to make calls to API endpoints and see their respective JSON response texts.

How to list all the videos posted by a youtube channel?
See more ...