【问题标题】:YouTube API v3 - List uploaded videosYouTube API v3 - 列出上传的视频
【发布时间】:2012-10-07 11:13:48
【问题描述】:

如何在 V3 api 中列出用户上传的视频?

【问题讨论】:

  • 使用 mine 参数列出频道/播放列表,但我找不到列出频道视频的方法

标签: youtube-api youtube-data-api


【解决方案1】:

如果您使用的是客户端,那么 Greg 的回答是正确的。要对基本请求执行相同的操作,请发出以下 2 个请求:

  1. 获取https://www.googleapis.com/youtube/v3/channels

    带参数:

    part=contentDetails
    mine=true
    key={YOUR_API_KEY}
    

    和标题:

    Authorization:  Bearer {Your access token}
    

    从这里你会得到一个像这样的 JSON 响应:

    {
     "kind": "youtube#channelListResponse",
     "etag": "\"some-string\"",
     "pageInfo": {
      "totalResults": 1,
      "resultsPerPage": 1
     },
     "items": [
      {
       "id": "some-id",
       "kind": "youtube#channel",
       "etag": "\"another-string\"",
       "contentDetails": {
        "relatedPlaylists": {
         "likes": "channel-id-for-your-likes",
         "favorites": "channel-id-for-your-favorites",
         "uploads": "channel-id-for-your-uploads",
         "watchHistory": "channel-id-for-your-watch-history",
         "watchLater": "channel-id-for-your-watch-later"
        }
       }
      }
     ]
    }
    

    您想从中解析出“上传”频道 ID。

  2. 获取https://www.googleapis.com/youtube/v3/playlistItems

    带参数:

    part=snippet
    maxResults=50
    playlistId={YOUR_UPLOAD_PLAYLIST_ID}
    key={YOUR_API_KEY}
    

    和标题:

    Authorization:  Bearer {YOUR_TOKEN}
    

    您将收到如下 JSON 响应:

    {
     "kind": "youtube#playlistItemListResponse",
     "etag": "\"some-string\"",
     "pageInfo": {
      "totalResults": 1,
      "resultsPerPage": 50
     },
     "items": [
      {
    
       "id": "some-id",
       "kind": "youtube#playlistItem",
       "etag": "\"another-string\"",
       "snippet": {
        "publishedAt": "some-date",
        "channelId": "the-channel-id",
        "title": "video-title",
        "thumbnails": {
         "default": {
          "url": "thumbnail-address"
         },
         "medium": {
          "url": "thumbnail-address"
         },
         "high": {
          "url": "thumbnail-address"
         }
        },
        "playlistId": "upload-playlist-id",
        "position": 0,
        "resourceId": {
         "kind": "youtube#video",
         "videoId": "the-videos-id"
        }
       }
      }
     ]
    }
    

使用这种方法,您应该能够使用任何语言甚至只是 curl 来获取信息。如果您想要超过前 50 个结果,则必须使用第二个请求执行多个查询并传入页面请求。更多信息请阅读:http://developers.google.com/youtube/v3/docs/playlistItems/list

【讨论】:

【解决方案2】:

第一步是获取该用户的频道 ID。我们可以通过请求Channels 服务来做到这一点。这是一个 JS 示例。

var request = gapi.client.youtube.channels.list({
  // mine: true indicates that we want to retrieve the channel for the authenticated user.
  mine: true,
  part: 'contentDetails'
});
request.execute(function(response) {
  playlistId = response.result.channels[0].contentDetails.uploads;
});

一旦我们获得播放列表 ID,我们就可以使用它来查询来自 PlaylistItems 服务的上传视频列表。

var request = gapi.client.youtube.playlistItems.list({
  playlistId: playlistId,
  part: 'snippet',
});
request.execute(function(response) {
  // Go through response.result.playlistItems to view list of uploaded videos.
});

【讨论】:

  • 对我来说,.list 方法不会返回。我有两个简单的日志,在调用方法之前和之后,第一个执行,第二个不执行。有什么想法吗?
猜你喜欢
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2016-08-27
  • 2018-06-11
  • 2017-06-14
  • 2014-10-13
  • 2014-12-30
  • 1970-01-01
相关资源
最近更新 更多