The YouTube Data API (v3) lets you incorporate YouTube functionality into your own application. You can use the API to fetch search results and to retrieve, insert, update, and delete resources like videos or playlists.
This blog post gives a quick overview of sending queries using the Obj-C Library. Lets see how to get all videos for a channel with known id. First, we will send a list channel
query with our channel identifier to load the channel info. Then we execute a list playlist items
query to find all videos in the playlists. Here’s some code:
- (void)loadChannels {
GTLQueryYouTube *channelsQuery = [GTLQueryYouTube queryForChannelsListWithPart:@"id,snippet,brandingSettings,contentDetails,invideoPromotion,statistics,status,topicDetails"];
channelsQuery.identifier = @"<CHANNEL_IDENTIFIER>";
[self.youTubeService executeQuery:channelsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeChannelListResponse *channelListResponse, NSError *error) {
if (error == nil) {
if (channelListResponse.items.count > 0) {
GTLYouTubeChannel *channel = channelListResponse.items.firstObject;
NSLog(@"Got youtube channel - %@", channel);
[self loadPlaylistsForChannel:channel];
} else {
NSLog(@"Cannot find channels with id %@", channelsQuery.identifier);
}
} else {
NSLog(@"Cannot get youtube channels - %@", error);
}
}];
}
- (void)loadPlaylistsForChannel:(GTLYouTubeChannel *)channel {
NSString *playlistId = channel.contentDetails.relatedPlaylists.uploads;
if (playlistId) {
GTLQueryYouTube *playlistsQuery = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet,contentDetails,status"];
playlistsQuery.playlistId = playlistId;
[self.youTubeService executeQuery:playlistsQuery completionHandler:^(GTLServiceTicket *ticket, GTLYouTubePlaylistItemListResponse *playlistListResponse, NSError *error) {
if (error == nil) {
if (playlistListResponse.items.count > 0) {
self.playlistItems = playlistListResponse.items;
[self.swipeView reloadData];
for (GTLYouTubePlaylistItem *playlistItem in playlistListResponse.items) {
NSLog(@"Got youtube playlist items - %@", playlistItem);
}
[self loadVideoForPlaylistItem:playlistListResponse.items.firstObject];
} else {
NSLog(@"Cannot find playlists items with playlist id %@", playlistId);
}
} else {
NSLog(@"Cannot get youtube playlist items - %@", error);
}
}];
} else {
NSLog(@"Playlist 'uploads' not found for channel.");
}
}
There are several queries available from the API. For the complete list, head over to the API Reference.