Search code examples
iosiphoneobjective-cyoutube-apigdata-api

How to upload video on YouTube form ios app? By Using Oauth2 and YouTube GDATA classes


I am working on app in which user can upload video to YouTube. All working fine until I move my login feature from the client login to oauth2. Now I can successfully authorize user and get ACCESS TOKEN, but I don't know how to use this ACCESS TOKEN to upload video in GDATA API methods.

in GDATA i am using some methodswhich use username and password. but now i dotn have username and password all i have is access_token

- (void)uploadVideoFile :(GTMOAuth2Authentication*)auth{
[mTitleField setText: [shareVidTitleArr objectAtIndex:0]];
[mDescriptionField setText: [shareVidDescpArr objectAtIndex:0]];
[mKeywordsField setText: @"Hi"];
[mCategoryField setText: @"Entertainment"];

NSString *devKey = [mDeveloperKeyField text];

GDataServiceGoogleYouTube *service = [self youTubeService];
[service setYouTubeDeveloperKey:devKey];

NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:kGDataServiceDefaultUser];
// NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:clientID];


// load the file data
NSArray *searchPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docDir = [searchPath objectAtIndex:0];
NSString  *path = [docDir stringByAppendingPathComponent:[shareVidArr objectAtIndex:0]];   

NSData *data = [NSData dataWithContentsOfFile:path];


NSString *filename = [path lastPathComponent];
NSLog(@"%@",filename);   



// gather all the metadata needed for the mediaGroup
NSString *titleStr = [mTitleField text];
GDataMediaTitle *title = [GDataMediaTitle textConstructWithString:titleStr];

NSString *categoryStr = [mCategoryField text];
GDataMediaCategory *category = [GDataMediaCategory mediaCategoryWithString:categoryStr];
[category setScheme:kGDataSchemeYouTubeCategory];

NSString *descStr = [mDescriptionField text];
GDataMediaDescription *desc = [GDataMediaDescription textConstructWithString:descStr];

NSString *keywordsStr = [mKeywordsField text];
GDataMediaKeywords *keywords = [GDataMediaKeywords keywordsWithString:keywordsStr];

BOOL isPrivate = mIsPrivate;

GDataYouTubeMediaGroup *mediaGroup = [GDataYouTubeMediaGroup mediaGroup];
[mediaGroup setMediaTitle:title];
[mediaGroup setMediaDescription:desc];
[mediaGroup addMediaCategory:category];
[mediaGroup setMediaKeywords:keywords];
[mediaGroup setIsPrivate:isPrivate];
//[mediaGroup setProperty:filename forKey:@"vidID"];

NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:path
                                           defaultMIMEType:@"video/quicktime"];

// create the upload entry with the mediaGroup and the file data
GDataEntryYouTubeUpload *entry;
entry = [GDataEntryYouTubeUpload uploadEntryWithMediaGroup:mediaGroup
                                                      data:data
                                                  MIMEType:mimeType
                                                      slug:filename];

SEL progressSel = @selector(ticket:hasDeliveredByteCount:ofTotalByteCount:);

[service setServiceUploadProgressSelector:progressSel];

GDataServiceTicket *ticket;
ticket = [service fetchEntryByInsertingEntry:entry
                                  forFeedURL:url
                                    delegate:self
                           didFinishSelector:@selector(uploadTicket:finishedWithEntry:error:)];

[self setUploadTicket:ticket];

}


Solution

  • Here's the complete example: https://github.com/youtube/yt-direct-lite-iOS/blob/master/YouTube%20Direct%20Lite%20for%20iOS/YouTubeUploadVideo.m

    It uses Data API v3, which is the new API, GData will be deprecated really soon.