Objective c iOS YouTube视频上载错误
我正在尝试使用Objective-C和YouTube API上传视频,但它不工作,并在最后一步返回错误。错误显示为“需要用户身份验证” 我特别关注这个没有元数据的问题。我得到了身份验证令牌 我用NSLog检查了身份验证令牌,它就在那里。我看到上传API也返回上传URL,但当我向检索到的上传URL发送HTTP PUT请求时,它返回上面提到的错误 这是上传代码Objective c iOS YouTube视频上载错误,objective-c,ios,youtube,Objective C,Ios,Youtube,我正在尝试使用Objective-C和YouTube API上传视频,但它不工作,并在最后一步返回错误。错误显示为“需要用户身份验证” 我特别关注这个没有元数据的问题。我得到了身份验证令牌 我用NSLog检查了身份验证令牌,它就在那里。我看到上传API也返回上传URL,但当我向检索到的上传URL发送HTTP PUT请求时,它返回上面提到的错误 这是上传代码 - (bool) upload:(NSString *)file { NSData *fileData = [NSData data
- (bool) upload:(NSString *)file {
NSData *fileData = [NSData dataWithContentsOfFile:file];
NSURL *url = [NSURL URLWithString:self.UploadURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"PUT"];
[request setValue:@"Content-Type" forHTTPHeaderField:@"application/octet-stream"];
[request setValue:@"Content-Length" forHTTPHeaderField:[NSString stringWithFormat:@"%ud", [fileData length]]];
[request setHTTPBody:fileData];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(@"%@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
if (response == nil) {
return NO;
} else {
return YES;
}
}
我还尝试了直接上传方法,但这总是给我无效请求错误。下面是代码
- (bool) directUpload:(NSString *)file {
NSString *title = [file lastPathComponent];
NSString *desc = @"This is test video.";
NSString *category = @"People";
NSString *keywords = @"video";
NSString *boundary = @"--qwerty";
NSString *xml = [NSString stringWithFormat:
@"<?xml version=\"1.0\"?>"
@"<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:media=\"http://search.yahoo.com/mrss/\" xmlns:yt=\"http://gdata.youtube.com/schemas/2007\">"
@"<media:group>"
@"<media:title type=\"plain\">%@</media:title>"
@"<media:description type=\"plain\">%@</media:description>"
@"<media:category scheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">%@</media:category>"
@"<media:keywords>%@</media:keywords>"
@"</media:group>"
@"</entry>", title, desc, category, keywords];
NSData *fileData = [NSData dataWithContentsOfFile:file];
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"%@\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: application/atom+xml; charset=UTF-8\n\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"%@\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Type: video/mp4\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"Content-Transfer-Encoding: binary\n\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:fileData];
[postBody appendData:[[NSString stringWithFormat:@"%@", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:@"http://uploads.gdata.youtube.com/feeds/api/users/default/uploads"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"GoogleLogin auth=\"%@\"", self.AuthToken] forHTTPHeaderField:@"Authorization"];
[request setValue:@"2" forHTTPHeaderField:@"GData-Version"];
[request setValue:[NSString stringWithFormat:@"key=%@", self.DeveloperKey] forHTTPHeaderField:@"X-GData-Key"];
[request setValue:[file lastPathComponent] forHTTPHeaderField:@"Slug"];
[request setValue:[NSString stringWithFormat:@"multipart/related; boundary=\"%@\"", boundary] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%ud", [postBody length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"close" forHTTPHeaderField:@"Connection"];
[request setHTTPBody:postBody];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(@"%@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
if (response == nil) {
return NO;
} else {
return YES;
}
}
-(bool)directUpload:(NSString*)文件{
NSString*title=[文件lastPathComponent];
NSString*desc=@“这是测试视频。”;
NSString*category=@“人”;
NSString*关键字=@“视频”;
NSString*边界=@“-qwerty”;
NSString*xml=[NSString stringWithFormat:
@""
@""
@""
@"%@"
@"%@"
@"%@"
@"%@"
@""
@“”、标题、说明、类别、关键字];
NSData*fileData=[NSData dataWithContentsOfFile:file];
NSMutableData*postBody=[NSMutableData];
[postBody appendData:[[NSString stringWithFormat:@“%@\n”,边界]数据使用编码:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“内容类型:应用程序/atom+xml;字符集=UTF-8\n\n”]dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[xml dataUsingEncoding:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“%@\n”,边界]数据使用编码:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“内容类型:视频/mp4\n”]数据使用编码:NSUTF8StringEncoding];
[postBody appendData:[[NSString stringWithFormat:@“内容传输编码:二进制\n\n”]dataUsingEncoding:NSUTF8StringEncoding];
[postBody appendData:fileData];
[postBody appendData:[[NSString stringWithFormat:@“%@”,边界]数据使用编码:NSUTF8StringEncoding];
NSURL*url=[NSURL URLWithString:@”http://uploads.gdata.youtube.com/feeds/api/users/default/uploads"];
NSMutableURLRequest*请求=[NSMutableUrlRequestRequestWithURL:url];
[请求设置HttpMethod:@“POST”];
[request setValue:[NSString stringWithFormat:@“GoogleLogin auth=\“%@\”,self.AuthToken]forHTTPHeaderField:@“Authorization”];
[请求设置值:@“2”用于HttpHeaderField:@“GData版本”];
[request setValue:[NSString stringWithFormat:@“key=%@”,self.DeveloperKey]用于HttpHeaderField:@“X-GData-key”];
[request setValue:[file lastPathComponent]forHTTPHeaderField:@“Slug”];
[request setValue:[NSString stringWithFormat:@“多部分/相关;边界=\“%@\”,边界]用于HttpHeaderField:@“内容类型”];
[request setValue:[NSString stringWithFormat:@“%ud”,[postBody length]]forHTTPHeaderField:@“内容长度”];
[请求设置值:@“关闭”HttpHeaderField:@“连接”];
[请求setHTTPBody:postBody];
N错误*请求错误;
NSURLResponse*urlResponse=nil;
NSData*response=[NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse错误:&requestError];
NSLog(@“%@,[[NSString alloc]initWithData:响应编码:NSUTF8StringEncoding]);
如果(响应==nil){
返回否;
}否则{
返回YES;
}
}
我想你应该检查一下:-
您还可以检查以下内容:-