【问题标题】:Youtube video upload using NSURLRequest使用 NSURLRequest 上传 Youtube 视频
【发布时间】:2013-11-11 00:05:24
【问题描述】:

我正在尝试使用 NSURLRequest 将视频上传到 youtube。 我能够进行身份验证,并且用户和视频都已上传,当检查时显示 Failed (unable to convert video file) 我不知道该怎么办,请使用

构造请求

Authorization = "Bearer ya29.AHES6ZTQ3rJZaf2g3pIYa_7_myg1N_GvQ4VdmJIcapfoqKIfQf-Iow";
Connection = close;
"Content-Length" = 1848078;
"Content-Type" = "multipart/related; boundary=217NH17UDP";
"GData-Version" = 2;
Host = "uploads.gdata.youtube.com";
Slug = "videoFile.mp4";
"X-GData-Key" = "key=AI39si4b-ta8ku-hbsLt73O0rIlOBjpHbITt8WGrwL7OevwjNjl5EFcowlJlBM6kp3rrU1cw64Vobp1l1lJP31Rqavshl4962A";

--217NH17UDP
Content-Type: application/atom+xml; charset=UTF-8

<?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">Sample Video File</media:title>
    <media:description type="plain">
        Description of the sample video
    </media:description>
    <media:category
        scheme="http://gdata.youtube.com/schemas/2007/categories.cat">Music
    </media:category>
    <media:keywords>Keywords</media:keywords>
</media:group>
</entry>
--217NH17UDP
Content-Type: video/mp4
Content-Transfer-Encoding: binary

Video data (NSData)

--217NH17UDP--

在“https://developers.google.com/youtube/2.0/developers_guide_protocol_direct_uploading#Direct_uploading”中指定,

请建议...

【问题讨论】:

    标签: ios video youtube youtube-api


    【解决方案1】:

    您可能想将 content_type 设置为 video/*

    【讨论】:

    • 谢谢,这不是原因,它现在工作正常。实际上我错过了帖子的格式,它甚至会检查帖子数据中的单个空格差异是否被视为无效,
    【解决方案2】:

    只有当你完全按照指定的方式实现请求时它才能正常工作(即使是一个空格也很重要)

    终于可以上传视频了,这里是 200% 工作代码。

    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];
                                   }
                               }];
    }
    

    【讨论】:

      猜你喜欢
      • 2011-06-22
      • 2012-12-30
      • 1970-01-01
      • 2012-07-19
      • 2016-01-11
      • 1970-01-01
      • 2013-02-01
      • 2014-12-26
      • 1970-01-01
      相关资源
      最近更新 更多