I am given a list of youtube IDs. For each of this IDS, I want to use the YouTube API to get the title, duration, and thumbnail. I then want the user to be able to click these thumbnails which takes them to a UIWebView where they can watch the video.
I'm really stuck on how I can actually create a video object from the ID. I dont really understand the process in given in Google's sample code, they get the title based on a playlist. I dont want it based on a playlist, I want it based on a stand-alone ID or URL.
Using a playlist query, the code uses:
if ([playlistID length] > 0) {
GTLServiceYouTube *service = self.youTubeService;
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:#"snippet,contentDetails"];
query.playlistId = playlistID;
query.maxResults = 50;
_playlistItemListTicket = [service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLYouTubePlaylistItemListResponse *playlistItemList,
NSError *error) {
// Callback
_playlistItemList = playlistItemList;
_playlistFetchError = error;
_playlistItemListTicket = nil;
[self updateUI];
}];
}
Which I understand, but looking through GTLQueryYouTube I cant see anything that relates to a single video.
Does anybody have any ideas? Thanks!
Eventually I used:
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
// Services which do not require sign-in may need an API key from the
service.APIKey = #"myKey";
GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosListWithPart:#"id, snippet, fileDetails"];
query.identifier = #"xxxxxxxxxx, yyyyyyyyyy";
GTLServiceTicket *ticket = [service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
// This callback block is run when the fetch completes
if (error == nil) {
GTLYouTubeSearchListResponse *products = object;
// iteration of items and subscript access to items.
for (GTLYouTubeSearchResult *item in products) {
//NSMutableDictionary *dictionary = [item JSONValueForKey:#"id"];
GTLYouTubeThumbnailDetails *thumbnails = item.snippet.thumbnails;
GTLYouTubeThumbnail *thumbnail = thumbnails.high;
[imageArray addObject:thumbnail.url];
[titleArray addObject:item.snippet.title];
//NSLog(#"id: %#", [dictionary objectForKey:#"videoId"]);
//NSLog(#"Title: %#", item.snippet.title);
}
}else{
NSLog(#"Error: %#", error.description);
}
[self.thumbnailView reloadData];
}];
So created a queryForVideosListWithPart using a list of video ids separated by comma
Related
i want to upload video to youtube channel.i tried with following Code.the bellow code work to upload video but the problem is, i can't attach its information(Video Title,Description & Tags).after uploading it shows only video title as a "unknown" and no tags or description .i've seen CodingTofu , Dev7Dev Blog- iOS Sample Code and many other Q&A but i can't understand where i'm making mistake.
NSString* title = video.title;
NSString* description = video.videoDescription;
NSData* data = video.data;
NSMutableArray* tags = video.tags.mutableCopy;
GTLYouTubeVideo *yVideo = [GTLYouTubeVideo object];
GTLYouTubeVideoSnippet *snippet = [GTLYouTubeVideoSnippet alloc];
snippet.title = title;
snippet.descriptionProperty = description;
[tags addObject:DEFAULT_KEYWORD];
NSString* tag = [UploadController generateKeywordFromPlaylistId:UPLOAD_PLAYLIST];
[tags addObject:tag];
snippet.tags = tags;
GTLYouTubeVideoStatus *status = [GTLYouTubeVideoStatus alloc];
status.privacyStatus = #"public";
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:#"video/*"];
GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosInsertWithObject:yVideo part:#"snippet,status" uploadParameters:uploadParameters];
service.uploadProgressBlock = ^(GTLServiceTicket* ticket, unsigned long long totalBytesUploaded, unsigned long long totalBytesExpectedToUpload){
CGFloat percentage = totalBytesUploaded/totalBytesExpectedToUpload;
NSLog(#"\nUploaded percentage %%(%0.2f)",percentage);
};
[service executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLYouTubeVideo *insertedVideo, NSError *error) {
//[waitIndicator dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil)
{
NSLog(#"File ID: %#", insertedVideo.identifier);
return;
}
else
{
NSLog(#"An error occurred: %#", error);
return;
}
}];
tell me if you need any further Details
thank you.
sorry for my bad English.
i got the Mistake,
in above code, i have to add
GTLYouTubeVideo *yVideo = [GTLYouTubeVideo object];
GTLYouTubeVideoStatus *status = [GTLYouTubeVideoStatus alloc];
GTLYouTubeVideoSnippet *snippet = [GTLYouTubeVideoSnippet alloc];
*******
*******
*******
yVideo.snippet = snippet;//This part i forgot to add in my code
yVideo.status = status;//This part i forgot to add in my code
[service executeQuery........
This Might be helpful to someone who facing same issue.
I'm trying to use the iOS-GTLYouTube library for displaying videos. So far this is what I've got:
//Get the youtube video list
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
// Set the APIKey
service.APIKey = #"APIKey";
GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosListWithIdentifier:[vidListIds copy] part:#"id,snippet"];
GTLServiceTicket *ticket = [ service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error)
{
// This callback block is run when the fetch completes
if (error == nil)
{
GTLYouTubeVideoListResponse *products = object;
// iteration of items and subscript access to items.
for (GTLYouTubeVideo *item in products)
{
GTLYouTubeVideoSnippetThumbnails *thumbnails = item.snippet.thumbnails;
[thumbnailMutableList addObject:thumbnails];
NSLog(#"Title: %#", item.snippet.title);
}
}
else
{
NSLog(#"Error: %#", error.description);
}
}];
I don't know why but this code didn't seem to do anything. There's neither the "Title:" log nor the "Error" log. I know that it was done in a separate thread so it worked asynchronously. But I've waited for 1-2 minutes and still nothing is shown. Can anybody help me here? Thanks.
Never mind. In the NSMutableString *vidListIds I forgot to append a comma. Silly me. Thanks for reading though. Sorry for the false alarm. :)
I would like to know the best way to get passed the video maxResults. I can't set it past 50. I need to get about 207 videos right now. This list will continue to grow over time.
Do you have any suggestions on how I would do this?
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = #"API Key";
service.shouldFetchInBackground = YES;
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:#"snippet,contentDetails"];
query.playlistId = #"Playlist ID";
query.maxResults = 50;
GTLServiceTicket *ticket = [service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
// This callback block is run when the fetch completes
if (error == nil)
{
GTLYouTubeSearchListResponse *products = object;
GTLYouTubePlaylistItemListResponse *playlistItems = object;
for (GTLYouTubeSearchResult *item in products)
{
GTLYouTubeThumbnailDetails *thumbnails = item.snippet.thumbnails;
GTLYouTubeThumbnail *thumbnail = thumbnails.high;
NSString *thumbnailString = thumbnail.url;
if (thumbnailString != nil)
{
[self.thumbnailsURL addObject:thumbnailString];
[self.thumbnailsArray addObject:thumbnailString];
[self.thumbnailImages addObject:#"120x120#2x.png"];
[self.thumbnailTitleArray addObject:item.snippet.title];
}
}
for (GTLYouTubePlaylistItem *playlistItem in playlistItems)
{
GTLYouTubePlaylistItemContentDetails *details = playlistItem.contentDetails;
[self.videos addObject:details.videoId];
}
}
else
{
NSLog(#"Error: %#", error.description);
}
self.loading.hidden = YES;
[self.tableView reloadData];
}];
Thank you in advance for your time! :D
You'll get the rest of the results with using paging.
YouTube API supports pagination. You need to put your nextPageToken in your next request as pageToken to get the next page(50 in your case) of results.
These 2 answers of mine should help you:
page tokens use youtube api v3
Retrieve all videos from youtube playlist using youtube v3 API
I'm trying to make a request for playListItems using the YouTube iOS SDK.
I'm getting back an error...
Error Domain=com.google.GTLJSONRPCErrorDomain
Code=-32602 "The operation couldn’t be completed. (No filter selected.)"
UserInfo=0x1568f1e0
{
error=No filter selected.,
GTLStructuredError=GTLErrorObject 0x1567fdf0: {message:"No filter selected." code:-32602 data:[1]},
NSLocalizedFailureReason=(No filter selected.)
}
The query I'm using is this...
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = #"my key";
// I suspect the problem is here...
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:#"contentDetails"];
query.q = #"playlistId=<The playlist ID>";
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);
}
}];
The problem is that the docs for the entire API are useless so there is no example of this being used. I'm having to put it together from the example for the Google Shopping apis and stuff.
Most of the guess work came from here.
Any ideas what I should put in the query.q value? Or if there's something else I'm missing?
OK, I finally found a link that shed some link on this.
The code to use is...
query.playlistId = #"the playlist ID";
Then everything worked :D
There are a lot of GData YouTube tutorials on how to upload a Video in an iOS App, but I didn't find one showing how to comment on a Video. So I read on the reference-page, just tried and so on but didn't find anything!
Does anyone know how to comment on a Video in an iOS App using the GData API?
Thanks
Use this methods in comment button
GDataEntryYouTubeVideo *video = (GDataEntryYouTubeVideo *)arrayData;
[self addCommentTitle:#"comment" text:commentField.text toVideo:video];
- (void)addCommentTitle:(NSString *)commentTitle
text:(NSString *)commentContent
toVideo:(GDataEntryYouTubeVideo *)entry {
GDataComment *commentObj = [entry comment];
GDataFeedLink *feedLink = [commentObj feedLink];
NSURL *feedURL = [feedLink URL];
if (feedURL) {
// fetch the comment feed for the video
GDataServiceGoogleYouTube *service = [self youTubeService];
[service setYouTubeDeveloperKey:devKey];
[service setAuthToken:[self getRequestToken]];
[service fetchFeedWithURL:feedURL completionHandler:^(GDataServiceTicket *ticket, GDataFeedBase *commentFeed,NSError *error) {
if (error == nil) {
GDataEntryYouTubeComment *newCommentEntry = [GDataEntryYouTubeComment commentEntry];
[newCommentEntry addContentValueDeclaration];
[newCommentEntry setTitleWithString:commentTitle];
[newCommentEntry setContentWithString:commentContent];
NSString *subString = [videoString substringWithRange: NSMakeRange(0, [videoString rangeOfString: #"?"].location)];
NSString *last=[subString lastPathComponent];
NSString *ss=#"http://gdata.youtube.com/feeds/api/videos/";
NSString *idd=#"/comments";
NSString *com=[NSString stringWithFormat:#"%#%#%#",ss,last,idd];
NSURL *postURL = [NSURL URLWithString:com ];
[service fetchEntryByInsertingEntry:newCommentEntry
forFeedURL:postURL
completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {
// callback
if (error == nil) {
NSLog(#"url.: succeeded ");
}
}];
}
}];
}
}
get Authentication token using login details
- (NSString*) getRequestToken {
// return your auth token as string
}