【问题标题】:How to upload video on YouTube form ios app? By Using Oauth2 and YouTube GDATA classes如何从 ios 应用程序在 YouTube 上上传视频?通过使用 Oauth2 和 YouTube DATA 类
【发布时间】:2014-03-19 16:38:14
【问题描述】:

我正在开发用户可以将视频上传到 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];

}

【问题讨论】:

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


    【解决方案1】:

    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”];
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 我需要完全实现你的api还是只使用你在添加源文件后提供的链接的一种方法?
      • 如何在我的项目中集成你的api和google api
      • 主视图中只有 OAuth2 部分,这种方法就足够了。
      • 如果你能指导我,我会非常感谢。我已经做的是成功实施 Ouath2 api 现在我获得了访问令牌。我认为我必须将访问令牌分配给 GDataServiceGoogleYouTube 对象。和其他东西会重新发送相同但没有成功现在它给了我 Error Domain=com.google.GDataServiceDomain Code=403 “操作无法完成。(com.google.GDataServiceDomain 错误 403。)” UserInfo=0x15dd9760 {Error=BadAuthentication, error=BadAuthentication}
      • 如何为新用户创建 youtube 频道?因为没有频道我们无法上传视频。
      猜你喜欢
      • 2018-09-30
      • 2017-04-13
      • 2017-08-31
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 2021-07-09
      • 1970-01-01
      相关资源
      最近更新 更多