I am trying to upload the video to youtube using NSURLRequest. I am able to authenticate and the user and also video gets uploaded, and when checked it says Failed (
It works fine only if you implement the request exactly as specified (even a single space matters)
Finally able to upload the video, Here the 200% working code.
NSString* const kMetaData = @"<?xml version=\"1.0\"?>\r\n<entry xmlns=\"http://www.w3.org/2005/Atom\"\r\nxmlns:media=\"http://search.yahoo.com/mrss/\"\r\nxmlns:yt=\"http://gdata.youtube.com/schemas/2007\">\r\n<media:group>\r\n<media:title type=\"plain\">%@</media:title>\r\n<media:description type=\"plain\">\r\n%@\r\n</media:description>\r\n<media:category\r\nscheme=\"http://gdata.youtube.com/schemas/2007/categories.cat\">%@\r\n</media:category>\r\n<media:keywords>%@</media:keywords>\r\n</media:group>\r\n</entry>\r\n";
- (void)uploadVideo:(NSString *)videoFilePath videoInfo:(NSDictionary*)inVideoInfo
{
NSData* videoData = [NSData dataWithContentsOfFile:videoFilePath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kVideoUploadURL]];
request.HTTPMethod = @"POST";
//header values
[request setValue:kHostHeaderFiels_Upload_Value forHTTPHeaderField:kHostHeaderField_Key];
[request setValue:[NSString stringWithFormat:@"%@ %@", self.tokenType, self.accessToken] forHTTPHeaderField:kAuthorizationHeaderField_Key];
[request setValue:@"2" forHTTPHeaderField:kGdataVersionHeaderField_Key];
[request setValue:videoFilePath.lastPathComponent forHTTPHeaderField:kSlugHeaderField_key];
[request setValue:[NSString stringWithFormat:kContentTypeHeaderField_Upload_Value, kBoundary_Value] forHTTPHeaderField:kContentTypeHeaderField_Key];
[request setValue:kConnectionHeaderField_Value forHTTPHeaderField:kConnectionHeaderField_Key];
[request setValue:kGdataKeyHeaderField_Value forHTTPHeaderField:kGdataKeyHeaderField_Key];
//Post Data
NSMutableData* httpBodyData = [NSMutableData data];
[httpBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", kBoundary_Value] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBodyData appendData:[@"Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *title, *descrition, *keywords, *category;
title = [inVideoInfo objectForKey:kVideoTitle];
descrition = [inVideoInfo objectForKey:kVideoDescription];
keywords = [inVideoInfo objectForKey:kVideoKeywords];
category = [inVideoInfo objectForKey:kVideoCategory];
NSString* metaData = kMetaData;
metaData = [NSString stringWithFormat:metaData, title, descrition, category, keywords];
[httpBodyData appendData:[metaData dataUsingEncoding:NSUTF8StringEncoding]]; //Meta Data
[httpBodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", kBoundary_Value] dataUsingEncoding:NSUTF8StringEncoding]]; //boundary string
[httpBodyData appendData:[[NSString stringWithFormat:@"Content-Type: video/%@\r\n", videoFilePath.pathExtension] dataUsingEncoding:NSUTF8StringEncoding]];
[httpBodyData appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[httpBodyData appendData:videoData];
[httpBodyData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", kBoundary_Value] dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:[NSString stringWithFormat:@"%u", httpBodyData.length] forHTTPHeaderField:kContentLengthHeaderField_Key];
request.HTTPBody = httpBodyData;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue new]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if(connectionError || data == nil)
{
self.uploadHandler(NO, nil);
}
else
{
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
xmlparser.delegate = self;
[xmlparser parse];
}
}];
}
You may wanna set content_type as video/*