YouTube API v3 未检索频道的视频

Posted

技术标签:

【中文标题】YouTube API v3 未检索频道的视频【英文标题】:YouTube API v3 not retrieving videos for a channel 【发布时间】:2015-07-22 21:35:21 【问题描述】:

好的,所以我使用以下 php 代码尝试让 youtube 返回用户视频列表,但是,他们的“项目”显示为空。

$apiKey         =   "MY-API-KEY";
$channel_name   =   "UCSzDPMGFGxDgM4YtNM4arTA";
$count          =   1;

$apiv3          =   json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=contentDetails&forUsername=".$channel_name."&key=".$apiKey));
foreach($apiv3->items as $item):
    $uploads = $item->contentDetails->relatedPlaylists->uploads;
endforeach;

$youtube        =   json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&contentDetails&maxResults=".$count."&playlistId=".$uploads."&key=".$apiKey));       

return $youtube;

这就是返回的内容:


 "kind": "youtube#channelListResponse",
 "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/3cxjRXf86G9z5Bg7rup3QfCgrxM\"",
 "pageInfo": 
  "totalResults": 0,
  "resultsPerPage": 5
 ,
 "items": [
 ]

然而,在他们的页面上,他们已经上传了视频。我怎样才能让它工作?我在另一个用户上使用了完全相同的代码,它工作正常。

【问题讨论】:

youtube.com/channel/UCSzDPMGFGxDgM4YtNM4arTA/videos 是他们 youtube 视频页面的链接。 您想要该频道发布的所有视频? 是的,这就是我所希望的。 【参考方案1】:

您可以改用视频搜索 API。使用带有channelId 参数的list 方法。抓住这个网址:

https://www.googleapis.com/youtube/v3/search?
part=snippet&channelId=THE_CHANNEL_ID&key=YOUR_API_KEY

响应将如下所示(从 Try It 部分复制):


 "kind": "youtube#searchListResponse",
 "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/28IrYJUKXsyiTkjfJRPDVrAyaLo\"",
 "nextPageToken": "CAUQAA",
 "pageInfo": 
  "totalResults": 9,
  "resultsPerPage": 5
 ,
 "items": [
  
   "kind": "youtube#searchResult",
   "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/u8AoS8t4dI9bLdKn4n1eMb9XQeY\"",
   "id": 
    "kind": "youtube#channel",
    "channelId": "UCSzDPMGFGxDgM4YtNM4arTA"
   ,
   "snippet": 
    "publishedAt": "2013-03-02T00:26:17.000Z",
    "channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
    "title": "Iron Hulk",
    "description": "",
    "thumbnails": 
     "default": 
      "url": "https://lh4.googleusercontent.com/-JeG2zOT2Eu8/AAAAAAAAAAI/AAAAAAAAAAA/9XlpZULoZ8E/photo.jpg"
     ,
     "medium": 
      "url": "https://lh4.googleusercontent.com/-JeG2zOT2Eu8/AAAAAAAAAAI/AAAAAAAAAAA/9XlpZULoZ8E/photo.jpg"
     ,
     "high": 
      "url": "https://lh4.googleusercontent.com/-JeG2zOT2Eu8/AAAAAAAAAAI/AAAAAAAAAAA/9XlpZULoZ8E/photo.jpg"
     
    ,
    "channelTitle": "",
    "liveBroadcastContent": "none"
   
  ,
  
   "kind": "youtube#searchResult",
   "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/sfhJOiYZgSFY3vR7MTCQqT5gR8c\"",
   "id": 
    "kind": "youtube#video",
    "videoId": "pqJGSSbII6E"
   ,
   "snippet": 
    "publishedAt": "2014-01-02T06:00:06.000Z",
    "channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
    "title": "Iron Hulk Fitness Group Sessions Compilation Jan 2014",
    "description": "A compilation of a few of the group sessions conducted in late 2013. Visit our facebook page for more information.",
    "thumbnails": 
     "default": 
      "url": "https://i.ytimg.com/vi/pqJGSSbII6E/default.jpg"
     ,
     "medium": 
      "url": "https://i.ytimg.com/vi/pqJGSSbII6E/mqdefault.jpg"
     ,
     "high": 
      "url": "https://i.ytimg.com/vi/pqJGSSbII6E/hqdefault.jpg"
     
    ,
    "channelTitle": "",
    "liveBroadcastContent": "none"
   
  ,
  
   "kind": "youtube#searchResult",
   "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/r91n5PeMndZnvf10n6T_L2dJYGg\"",
   "id": 
    "kind": "youtube#video",
    "videoId": "I8bpjJqTnRI"
   ,
   "snippet": 
    "publishedAt": "2013-10-27T05:55:13.000Z",
    "channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
    "title": "Iron Hulk Advanced plyometric bodyweight circuit 27/10/13",
    "description": "This weeks advanced class circuit focusing on plyometric and explosive movements 3 rounds for time: 12 x plyometric alternating hands push ups 8 x Alternating ...",
    "thumbnails": 
     "default": 
      "url": "https://i.ytimg.com/vi/I8bpjJqTnRI/default.jpg"
     ,
     "medium": 
      "url": "https://i.ytimg.com/vi/I8bpjJqTnRI/mqdefault.jpg"
     ,
     "high": 
      "url": "https://i.ytimg.com/vi/I8bpjJqTnRI/hqdefault.jpg"
     
    ,
    "channelTitle": "",
    "liveBroadcastContent": "none"
   
  ,
  
   "kind": "youtube#searchResult",
   "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/xrF0LOpaWmqPl4E2isJ7r4VL2Ds\"",
   "id": 
    "kind": "youtube#video",
    "videoId": "Y3bK24QHlxk"
   ,
   "snippet": 
    "publishedAt": "2014-11-15T19:07:35.000Z",
    "channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
    "title": "PORC Nov 2014",
    "description": "Iron Hulk's first PORC event. November 15th 2014.",
    "thumbnails": 
     "default": 
      "url": "https://i.ytimg.com/vi/Y3bK24QHlxk/default.jpg"
     ,
     "medium": 
      "url": "https://i.ytimg.com/vi/Y3bK24QHlxk/mqdefault.jpg"
     ,
     "high": 
      "url": "https://i.ytimg.com/vi/Y3bK24QHlxk/hqdefault.jpg"
     
    ,
    "channelTitle": "",
    "liveBroadcastContent": "none"
   
  ,
  
   "kind": "youtube#searchResult",
   "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/WQudUOFtaQNk4DyhMg7-Wx2tTAE\"",
   "id": 
    "kind": "youtube#video",
    "videoId": "TAgZL5OzVsU"
   ,
   "snippet": 
    "publishedAt": "2015-05-05T21:12:20.000Z",
    "channelId": "UCSzDPMGFGxDgM4YtNM4arTA",
    "title": "IHF 2015",
    "description": "What we've been up to in the first half of 2015.",
    "thumbnails": 
     "default": 
      "url": "https://i.ytimg.com/vi/TAgZL5OzVsU/default.jpg"
     ,
     "medium": 
      "url": "https://i.ytimg.com/vi/TAgZL5OzVsU/mqdefault.jpg"
     ,
     "high": 
      "url": "https://i.ytimg.com/vi/TAgZL5OzVsU/hqdefault.jpg"
     
    ,
    "channelTitle": "",
    "liveBroadcastContent": "none"
   
  
 ]

【讨论】:

太棒了!我用的是googleapis.com/youtube/v3/activities? part=contenDetails&channelId=THE_CHANNEL_ID&key=YOUR_API_KEY 并且它不起作用。这有效!【参考方案2】:

我认为部件属性应该用逗号分隔。

$youtube = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet,contentDetails&maxResults...));

【讨论】:

【参考方案3】:

@SalmanA 有一个可以使用搜索端点的答案;但是,如果您想使用 Uploads 播放列表,它也可以进行一些调整(只需通过 API 资源管理器确认)。首先,正如@SauminiNavaratnam 提到的,您的“部分”参数需要用逗号分隔。其次,当您检索频道详细信息时,您不能使用 forUsername 参数传入频道 ID。您需要使用 forUsername 参数传入 用户名,或者将频道 ID 作为 id 参数传入(请注意,您遇到的问题是 channelList 响应为空,并且因此没有任何东西可以传递给 playlistItems 调用)。如果您进行这些更改,您将获得正确的上传 ID,然后您的 playlistItems 调用将返回与搜索调用相同的结果。

但是,总而言之,您可能更喜欢搜索端点,因为它能够按日期、受欢迎程度等对结果进行排序。

【讨论】:

以上是关于YouTube API v3 未检索频道的视频的主要内容,如果未能解决你的问题,请参考以下文章

youtube api v3 从比视频更新的频道中获取所有视频

在 C# 中使用 Youtube API V3 从频道获取视频

YouTube Data v3 API - 如何从频道请求所有视频?

如何通过 YouTube Data API v3 从我自己的 YouTube 频道获取所有视频

如何在 YouTube API v3 上检索“频道中的频道”

YouTube API:未获取给定频道的所有视频