iOS从YouTube获取视频的JSON列表

时间:2013-08-06 21:15:36

标签: ios objective-c youtube

我以前从未使用过YouTube API,所以我完全不了解API文档......而且没有太多运气。

我设法找到了一些可以获取视频ID并在应用中播放的示例代码。这很好。

但是,我想获取用户ID(即频道ID)并获取视频ID列表(使用JSON格式)。

我正在查看v3文档...

Here I can get the details of the channel by using the channel ID

Here I can get a list of the playlists belonging to a channel by using the channel ID (but no videos)

Here I can get the details of a video but only by using the video ID. (or searching using a name, category, etc...

我想要做的是使用频道ID(我实际拥有频道)并获取视频列表,然后我可以在应用中显示它们,用户可以观看它们。

...使用JSON,而不是XML。

2 个答案:

答案 0 :(得分:3)

好的,所以两小时后我就完成了它:)

首先我安装了Cocoapods的iOS-GTLYouTube和SCFNetworking框架。

然后在app delegate ...

#import <GTLYouTube.h>

然后发出请求......

GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];

service.APIKey = @"my api key from google";

GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"contentDetails"];
query.playlistId = @"the playlist id I want to request";

GTLServiceTicket *ticket = [service executeQuery:query
                               completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
                                   if (!error) {
                                       GTLYouTubePlaylistItemListResponse *playlistItems = object;

                                       for (GTLYouTubePlaylistItem *playlistItem in playlistItems) {
                                           GTLYouTubePlaylistItemContentDetails *details = playlistItem.contentDetails;

                                           NSLog(@"PlaylistItem video ID = %@", details.videoId);
                                       }
                                   } else {
                                       NSLog(@"%@", error);
                                   }
                               }];

答案 1 :(得分:0)

在问题中,您列出了这些端点:

  • / V3 /信道
  • / V3 /视频
  • / V3 /播放列表

您找到的解决方案是调用此端点以获取播放列表中的视频:

  • / V3 / playlistItems

在YouTube的API资源管理器链接中:

https://developers.google.com/youtube/v3/docs/playlistItems/list

part设置为contentDetails,将playlistId设置为YouTube播放列表ID。

以下是回复的相关部分:

  {
   "pageInfo": {
    "totalResults": 83,
    "resultsPerPage": 5
   },
   "items": [
    {
     "contentDetails": {
      "videoId": "kGbDymJ75PU"
     }
    },
    {
     "contentDetails": {
      "videoId": "oVPepkniuXI"
     }
    }
   ]
  }