作者热门文章
- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 Objective-C 和 YouTube API 上传视频,但它无法正常工作并在最后一步返回错误。错误显示为“需要用户身份验证”。
我正在关注这个 API document特别是没有元数据的那个。我通过 ClientLogin API 获得了身份验证 token
我用 NSLog 检查了身份验证 token ,它就在那里。我看到上传 API 也返回上传 URL,但是当我向检索到的上传 URL 发送 HTTP PUT 请求时,它返回上述错误。
这是上传代码
- (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;
}
}
最佳答案
我认为你应该检查这个:-
https://developers.google.com/youtube/2.0/developers_guide_protocol_video_feeds
你也检查这个:-
http://urinieto.com/2010/10/upload-videos-to-youtube-with-iphone-custom-app/
关于objective-c - iOS YouTube 视频上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9632008/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!