我正在尝试使用youtube api的示例代码上传视频。当我按下上传按钮时,进度条完成其过程,但是一旦到达点的末尾,我就会出错。错误描述如下:
YouTubeTest[2149:f803] 错误 - 错误 Domain=com.google.GDataServiceDomain Code=400 "操作无法 完成。(com.google.GDataServiceDomain error 400.)" UserInfo=0x69d5bd0 {}
这是按下上传按钮的代码
- (IBAction)uploadPressed:(id)sender {
[self.view resignFirstResponder];
NSString *devKey = [mDeveloperKeyField text];
GDataServiceGoogleYouTube *service = [self youTubeService];
[service setYouTubeDeveloperKey:devKey];
NSString *username = [mUsernameField text];
NSString *clientID = [mClientIDField text];
NSURL *url = [GDataServiceGoogleYouTube youTubeUploadURLForUserID:username
clientID:clientID];
// load the file data
NSString *path = [[NSBundle mainBundle] pathForResource:@"YouTubeTest" ofType:@"m4v"];
NSData *data = [NSData dataWithContentsOfFile:path];
NSString *filename = [path lastPathComponent];
// 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];
NSString *mimeType = [GDataUtilities MIMETypeForFileAtPath:path
defaultMIMEType:@"video/mp4"];
// 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];
}
我已经从 API 仪表板正确设置了开发人员密钥和客户端密钥。
我正在模拟器上运行它。是我们不能从模拟器上传视频吗?
请指导我哪里出错了?
错误已解决...使用谷歌帐户将视频上传到YouTube时,GData的某些功能Objective-C需要Gmail帐户作为参数,有些需要YouTube关联的帐户作为参数。
当您调用"- (void)setUserCredentialsWithUsername:(NSString *)用户名密码:(NSString *)password;' 在 GDataServiceBase 中,用户名应该是Gmail帐户,例如"x...@gmail.com",密码应该是Gmail帐户。
但是当你调用'+ (NSURL *)youTubeUploadURLForUserID:(NSString *)userID clientID:(NSString *)clientID;' in GDataServiceGoogleYouTube,用户 ID 参数应为 YouTube 关联的帐户,并且密码 Gmail 帐户的密码。
我正在使用 email_id@gmail.com 登录,现在我只是使用email_id登录。多么愚蠢的错误..但是我花了整整2天的时间来解决。.咄..!!