首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何列出youtube频道发布的所有视频?

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

Stack Overflow用户
提问于 2019-06-02 09:24:28
回答 1查看 121关注 0票数 1

我正在做一个网站来配合我的youtube频道,我想在我的频道上的一个页面上发布所有最新视频的列表,类似于这个网站:http://hermitcraft.com/,但只针对一个特定的频道。

我是google apis系统的新手,我发现它压倒性地令人不知所措。

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

我该如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-02 14:49:13

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

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

暂时不必为OAuth身份验证而烦恼,只需从谷歌的developers console获取一个API key以便与API的PlaylistItems endpoint一起使用,即可查询给定的channel's uploads list

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

代码语言:javascript
复制
  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()));
  }

从get go开始,您必须知道应用程序接口正在实现的the quota system。根据使用模式的不同,配额可能会将rather tight limits设置为允许用户在API的各个端点上进行调用的数量。在任何时候,谷歌的开发者控制台都会向你显示你当前的配额。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56411594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档