We want to display list of channel's uploads. That list has an identifier, we need to find out, and it can be done with this request:
1. Channel details.
- (void) channelDetails {
Response is in this format:
item: {
googlePlusUserId = XXXXXXXXXX;
relatedPlaylists = {
uploads = "YYYYYYYYYYYYY";
};
}
Store the value from "uploads" field to UPLOADS_LIST_ID.
2. List of uploaded videos. To receive list of uploaded videos we need to make a playlist items query with UPLOADS_LIST_ID as playlist id.
3. List of uploaded videos with all information. Playlist queries, like one from above give us list of videos with details like: video id, title and thumbnail. To be able to read information like video duration and number of views, we need to make video list query. We can use list of video identifiers as filter for that query. For that reason we stored them in self.identifiers property of NSMutableDictionary type.
- (void) getVideos {
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosListWithPart:@"id, snippet, contentDetails, statistics, player"];
query.identifier = [[self.identifiers valueForKey:@"description"] componentsJoinedByString:@","];
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error){
if (!error) {
GTLYouTubeVideoListResponse *videolistItems = object;
for (GTLYouTubeVideo *videoItem in videolistItems){
[self.dataSource addObject:videoItem];
}
[self.tableView reloadData];
} else {
NSLog(@"%@", error);
}
}];
}
You may also need query for list of playlists for fetching all information from one channel. For this query we will need CHANNEL_ID, not name.
4. List of channel's playlists:
- (void) loadPlaylists {
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistsListWithPart:@"id, snippet, contentDetails"];
query.channelId = CHANNEL_ID;
query.maxResults = MAX_RESULTS;
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (!error) {
GTLYouTubePlaylistListResponse *items = object;
for (GTLYouTubePlaylist * item in items) {
[self.dataSource addObject:item];
}
[self.tableView reloadData];
} else {
NSLog(@"%@", error);
}
}];
}
1. Channel details.
- (void) channelDetails {
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForChannelsListWithPart:@"contentDetails"];
query.forUsername = CHANNEL_NAME;
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (!error) {
GTLYouTubeChannelListResponse *items = object;
for (GTLYouTubeChannelListResponse * item in items){
NSLog(@"item: %@", item.JSON[@"contentDetails"]);
}
} else{
NSLog(@"%@", error);
}
}];
}
Response is in this format:
item: {
googlePlusUserId = XXXXXXXXXX;
relatedPlaylists = {
uploads = "YYYYYYYYYYYYY";
};
}
Store the value from "uploads" field to UPLOADS_LIST_ID.
2. List of uploaded videos. To receive list of uploaded videos we need to make a playlist items query with UPLOADS_LIST_ID as playlist id.
- (void) getUploads {
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init]; service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
query.playlistId = UPLOADS_LIST_ID;
query.maxResults = MAX_RESULTS;
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init]; service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet"];
query.playlistId = UPLOADS_LIST_ID;
query.maxResults = MAX_RESULTS;
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (!error) {
GTLYouTubePlaylistItemListResponse *playlistItems = object;
for (GTLYouTubePlaylistItem *playlistItem in playlistItems) {
[self.identifiers addObject:playlistItem.snippet.resourceId.JSON[@"videoId"]];
}
[self getVideos];
}else {
NSLog(@"%@", error);
}
}];
}
3. List of uploaded videos with all information. Playlist queries, like one from above give us list of videos with details like: video id, title and thumbnail. To be able to read information like video duration and number of views, we need to make video list query. We can use list of video identifiers as filter for that query. For that reason we stored them in self.identifiers property of NSMutableDictionary type.
- (void) getVideos {
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosListWithPart:@"id, snippet, contentDetails, statistics, player"];
query.identifier = [[self.identifiers valueForKey:@"description"] componentsJoinedByString:@","];
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error){
if (!error) {
GTLYouTubeVideoListResponse *videolistItems = object;
for (GTLYouTubeVideo *videoItem in videolistItems){
[self.dataSource addObject:videoItem];
}
[self.tableView reloadData];
} else {
NSLog(@"%@", error);
}
}];
}
You may also need query for list of playlists for fetching all information from one channel. For this query we will need CHANNEL_ID, not name.
4. List of channel's playlists:
- (void) loadPlaylists {
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistsListWithPart:@"id, snippet, contentDetails"];
query.channelId = CHANNEL_ID;
query.maxResults = MAX_RESULTS;
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (!error) {
GTLYouTubePlaylistListResponse *items = object;
for (GTLYouTubePlaylist * item in items) {
[self.dataSource addObject:item];
}
[self.tableView reloadData];
} else {
NSLog(@"%@", error);
}
}];
}
this is a great tutorial.
ReplyDeletewhat is "self.identifiers" an NSArray
what is "self.dataSource" also an NSArray
please let me know thanks, that the only thing i don't understand here, but i believe that is because of my lack of knowledge not because the tutorial this is the only thing on the net about this that is clean and clear about youtube api v3 thanks again and please get back to me thank you
Hi, thanks for compliments.
Delete_identifiers is an (mutable) array of NSString objects - youtube video identifiers, e.g.: @[4uY7yLEVtIU, eSiPMQxPPXw, AGJIjSqndj0]
while _dataSource is an array of GTLYouTubeVideo objects.
Both need to be initialized before using in functions, e.g. in viewDidLoad:
self.identifiers = [[NSMutableArray alloc] init];
self.dataSource = [[NSMutableArray alloc] init];
Because GTLYouTubePlaylistItem doesn't have information about number of views and video duration I used query that takes list of identifiers and returns list of GTLYouTubeVideo objects, which have all those information.
Thanks so much for responding your tutorial is the only one that is in detail like this its the best one on the net i think thanks heaps
DeleteHow would you do a simple search. Thank you
ReplyDeleteSearch as in a keyword search. Sorry, I might not have been clear. Thank you
ReplyDeleteHi,
DeleteHere is the query for searching videos with KEYWORD:
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = API_KEY;
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id,snippet"];
query.q = KEYWORD;
query.maxResults = MAX_RESULTS;
query.type = @"video";
NSMutableArray *arrayOfIdentifiers = [[NSMutableArray alloc] init];
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error){
if (!error) {
GTLYouTubeSearchListResponse *videolistItems = object;
for (GTLYouTubeSearchResult *videoItem in videolistItems) {
[arrayOfIdentifiers addObject:videoItem.identifier.JSON[@"videoId"]];
}
self.identifiers = [NSArray arrayWithArray:arrayOfIdentifiers];
[self getVideos];
} else {
NSLog(@"%@", error);
}
}];
Thank.
DeleteAre you aware of this problem : https://code.google.com/p/gdata-issues/issues/detail?id=5770
Apikey doesnt seem to work.
As I understand, that is the problem with Google API configuration, not with the code itself.
DeleteAwesome article.
ReplyDeleteAny way to get videos in a channel using their tags?
ie. get all containing "TAG"
Sorry for late response. I'm not aware of any other solution than to add a tag as a q parameter (q: The q parameter specifies the query term to search for.). However, the response can have videos that doesn't contain specified term as a tag, but also as a part of a name.
DeleteThis comment has been removed by the author.
ReplyDeletei get his in getVideos Error Domain=com.google.GTLJSONRPCErrorDomain Code=-32602 "(No filter selected.)" ??
ReplyDeleteI suppose you didn't provide query.identifier (described in step 3). You can pass the array of video identifiers, if you want to get more details about the videos. However, if you need just a list of uploaded videos, consider using only the function described in step 2.
DeleteIts just a pin
ReplyDeleteI want to use GTLServiceYouTube in swift. Can you please help me how we can use this?. Or any other library like GTLServiceYouTube
ReplyDelete