ios - 如何从 ios 应用程序上传视频到 YouTube?通过使用 Oauth2 和 YouTube DATA 类

标签 ios iphone objective-c youtube-api gdata-api

我正在开发用户可以将视频上传到 YouTube 的应用。一切正常,直到我将登录功能从客户端登录移至 oauth2。 现在我可以成功授权用户并获取 ACCESS TOKEN,但我不知道如何使用此 ACCESS TOKEN 在 GDATA API 方法中上传视频。

在 GDATA 中,我正在使用一些使用用户名和密码的方法。但现在我没有用户名和密码,我所拥有的只是 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];

}

最佳答案

以下是完整示例:https://github.com/youtube/yt-direct-lite-iOS/blob/master/YouTube%20Direct%20Lite%20for%20iOS/YouTubeUploadVideo.m

它使用 Data API v3,这是新的 API,GData 很快就会被弃用。

关于ios - 如何从 ios 应用程序上传视频到 YouTube?通过使用 Oauth2 和 YouTube DATA 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21878958/

相关文章:

iphone - 在 NSArray 中搜索

ios - 在从 UICollectionView 删除单元格之前的项目之后删除动画

objective-c - 在 Objective-C 中,我们是否必须使用 self.var 或仅使用 var 来引用 self 的属性?

ios - 使用多个方案和推送通知配置配置文件

ios - 为什么我的预览图层没有覆盖整个 View ?

ios - 如何更改 imagepickercontroller ios 状态栏的颜色?

ios - NSString drawAtPoint : and kCGTextFillClip, 为什么不起作用?

iphone - 检测 AVAudioSession 何时为 "Ducked"

iphone - NSTimer 没有停止

ios - iOS中从服务器获取数据