如何从ios应用上传YouTube上的视频?使用Oauth2和YouTube GDATA课程

时间:2014-02-19 11:20:07

标签: 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];

}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

1)urinieto.com/iPhone/YouTubeTest.zip

2)然后添加您的devkey和客户端ID

3)删除gdata文件夹而不是添加GData-iOS-Static-Library-1.12-master

4)

//[service setShouldCacheDatedData:YES]; need to comment this line
[service setServiceShouldFollowNextLinks:YES];
[service setIsServiceRetryEnabled:YES];

5)

//NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username];
    NSURL *url = [[NSURL alloc] initWithString:@”http://uploads.gdata.youtube.com/resumable/feeds/api/users/default/uploads”];