首页 文章

YouTube API v3 - 列出上传的视频

提问于
浏览
33

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

2 回答

  • 40

    第一步是获取该用户的 Channels 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.
    });
    
  • 31

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

    带参数:

    part=contentDetails
    mine=true
    key={YOUR_API_KEY}
    

    和 Headers :

    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"
        }
       }
      }
     ]
    }
    

    从这里你想要解析“上传” Channels ID .

    带参数:

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

    和 Headers :

    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"
        }
       }
      }
     ]
    }
    

    使用这种方法,您应该能够使用任何语言获取信息,甚至只是卷曲 . 如果您想要超过前50个结果,那么您将不得不使用第二个请求进行多个查询并传入页面请求 . 有关详细信息,请参阅:http://developers.google.com/youtube/v3/docs/playlistItems/list

相关问题