【问题标题】:How to list all the videos posted by a youtube channel?如何列出 youtube 频道发布的所有视频?
【发布时间】:2019-10-18 01:52:17
【问题描述】:

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

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

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

我该如何解决这个问题?

【问题讨论】:

    标签: php video youtube youtube-api youtube-data-api


    【解决方案1】:

    假设您能够使用 PHP 进行编程,我建议您从小处着手,从 YouTube Data API OverviewPHP QuickstartPHP Client Library: Getting Started 逐步进行。无论如何,the reference documentation 是你最好的朋友——只是你必须熟悉它。

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

    暂时不用OAuth认证,只从谷歌的developers consoleAPI key获取与API的PlaylistItems endpoint一起使用,查询给定的channel's uploads list

    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 API 正在实施。根据使用模式,配额可能会将rather tight limits 置于允许用户在 API 的各个端点上进行的调用次数上。 Google 的开发者控制台会随时向您显示您当前的配额。

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

    【讨论】:

      猜你喜欢
      • 2018-11-29
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2015-09-02
      • 2016-05-08
      • 2011-09-13
      • 2012-12-31
      相关资源
      最近更新 更多