2012-03-09 2 views
4

Objective-C 및 YouTube API를 사용하여 동영상을 업로드하려고하는데 작동하지 않고 마지막 단계에서 오류를 반환합니다. 오류 "사용자 인증 필요"읽습니다.iOS YouTube 동영상 업로드 오류

다음은이 API document입니다. 특히 메타 데이터가없는 것입니다. ClientLogin API

NSLog로 인증 토큰을 확인했습니다. 거기에 있습니다. 업로드 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; 
    } 
} 

답변