如何列出 youtube 频道发布的所有视频?

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

我正在制作一个网站来配合我的 YouTube 频道,我想在网站的一个页面上列出我频道上发布的所有最新视频,类似于这个网站:http://hermitcraft.com/ 但仅限于一个特定频道。

我是 google api 系统的新手,发现它完全让人不知所措。

我的网络服务器是 apache 并运行 php 7,但由于它是网络主机,我无法访问任何控制台。

我该如何解决这个问题?

假设您能够在 PHP 中编程,我确实建议从小处着手并逐步进行 YouTube Data API Overview, PHP Quickstart and PHP Client Library: Getting Started. In any case, the reference documentation 是您最好的朋友——只是您必须熟悉它.

您将使用 PHP 客户端库 code,因此将其克隆到本地计算机。

暂时不用OAuth认证,只从Google的developers console an API key for to use it with API's PlaylistItems endpoint, queried for the given .

获取

Github 上有一些 sample code 用于获取用户的上传列表,但是该代码很旧并且很可能有问题(它也使用 OAuth 授权,我已经建议您使用不打扰)。这是该代码的基本部分(我对其进行了一些修改:将 'mine' => 'true' 替换为 'id' => $YOUR_CHANNEL_ID;不过您必须测试此代码):

  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()));
  }

从一开始,您就必须了解 the quota system the API is implementing. Depending on usage patterns, the quotas may put rather tight limits 允许用户在 API 的各个端点上进行的调用次数。在任何时候,Google 的开发者控制台都会显示您当前的配额。

最后,一个用于调试您的应用程序的有用工具:APIs Explorer。它允许您调用 API 端点并查看它们各自的 JSON 响应文本。