iphone - 在YouTube上上传视频

标签 iphone objective-c ios youtube asihttprequest

我需要将视频从我的iPhone应用程序上传到youtube,我已经获得了访问 token ,形式为google oauth,但是在将视频上传到youtube时遇到问题,我使用ASIFormDataRequest请求上传视频。以下是我的代码,请帮助我解决我的错误,在此先感谢。

- (IBAction)buttonTouched:(id)sender
{
    NSLog(@"%d %@",[sender tag],[[NSUserDefaults standardUserDefaults] valueForKey:@"access_token"]);

    progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(30, 90, 200, 20.0)];
    [alertView addSubview:progressView];
    [alertView show];
    [progressView setProgress:0];   

    NSString *name=[videosToShow objectAtIndex:[sender tag]];
    NSString *newFilePath = [NSString stringWithFormat:@"%@.mov", [self.path stringByAppendingPathComponent:name]];
    NSLog(@"Content URL :%@;", newFilePath);

    NSURL *url = [NSURL URLWithString:@"https://uploads.gdata.youtube.com/feeds/api/users/default/uploads"];

    ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];

    [request setRequestMethod:@"POST"];

    NSLog(@"Auth Token== %@",[[NSUserDefaults standardUserDefaults] valueForKey:@"access_token"]);

    [request setPostValue:[[NSUserDefaults standardUserDefaults] valueForKey:@"access_token"] forKey:@"authentication_token"];

    [request setPostValue:devKey forKey:@"developer_key"];

    [request setPostValue:@"abcabc" forKey:@"boundary_string"];

    [request setPostValue:devKey forKey:@"content_length"];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"xmlcontent" ofType:@"txt"];
    NSString *strXml = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    [request setPostValue:strXml forKey:@"API_XML_Request"];

    [request setPostValue:@"video/mov" forKey:@"video_content_type"];

    [request setPostValue:@"video/mov" forKey:@"Binary File Data"];

    [request setFile:newFilePath withFileName:@"1.mov" andContentType:@"video/mov" forKey:@"uploaded"];

    [request setUploadProgressDelegate:self];
    [request setDelegate:self];

    [request setDidFinishSelector:@selector(requestFinished:)];
    [request setDidFailSelector:@selector(requestFailed:)]; 

    [request setShowAccurateProgress:YES];
    [request startAsynchronous];
}

- (void)requestFinished:(ASIFormDataRequest *)request {
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Congrats!!!" message:@"File has been downloaded." 
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)requestFailed:(ASIFormDataRequest *)request1 {
    NSError *error = [request1 error];
    NSLog(@"%@", error);
}

以下是文件xmlcontent.txt
<?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">Bad Wedding Toast</media:title>
<media:description type="plain">
I gave a bad toast at my friend's wedding.
</media:description>
<media:category
scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
</media:category>
<media:keywords>toast, wedding</media:keywords>
</media:group>
</entry>

以下是我得到的错误

Error Domain=ASIHTTPRequestErrorDomain Code=3 "Authentication needed" UserInfo=0x1c8ed0 {NSLocalizedDescription=Authentication needed}

最佳答案

您正在使用GData库,但尚未链接/添加它。查看XCode 4中的GData Objective-C客户端安装程序,了解如何正确构建/链接GData库。

关于iphone - 在YouTube上上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13269027/

相关文章:

iphone - nib 文件未在 Xcode 4 中打开

ios - 如何将 XIB 文件添加到使用 Storyboard 的 iOS 应用程序?

objective-c - ObjC/核心数据。如何从另一个 NSArrayController 更改实体的键值?

ios - 如何在 Swift 中设置对闭包/函数的弱引用?

ios - iPhone慢动作视频转码

iphone - 如何在 xcode 中添加库项目?

ios - 自动布局 UIButton 之间的间距相等

iphone - NSDecimalNumber 用于 iPhone 上的大数运算

ios - UICollectionViewCell 未显示

ios - 为什么我在 GPU 上有很大的精度错误?