在YouTube上上传视频

时间:2012-11-07 11:40:06

标签: iphone objective-c ios youtube asihttprequest

我需要将视频从我的iphone应用程序上传到youtube,我有访问令牌形式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>

以下是我得到的错误

  

错误域= ASIHTTPRequestErrorDomain代码= 3“需要身份验证”   UserInfo = 0x1c8ed0 {NSLocalizedDescription =需要身份验证}

1 个答案:

答案 0 :(得分:0)

您正在使用GData库,但尚未链接/添加它。查看XCode 4中关于如何正确构建/链接GData库的GData Objective-C客户端设置。

相关问题