2016-08-10 4 views
1

に非上場のプライバシー設定で動画をアップロードするために、私はこのコードを使用してユーチューブに動画をアップロードしています。..どのようにYouTubeの

- (void)sendVideoFileMetadata:(NSDictionary *)videoMetadata 
         error:(NSError **)error 
{ 
    [self logDebug:@"Sending file info..."]; 

    NSString *category = videoMetadata[kDDYouTubeVideoMetadataCategoryKey]; 
    NSString *keywords = videoMetadata[kDDYouTubeVideoMetadataKeywordsKey]; 
    NSString *title = videoMetadata[kDDYouTubeVideoMetadataTitleKey]; 
    NSString *desc  = videoMetadata[kDDYouTubeVideoMetadataDescriptionKey]; 

    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:privacyStatus>unlisted</media:privacyStatus>" 
        @"</media:group>" 
        @"</entry>", title, desc, category, keywords]; 

    NSURL *url = [NSURL URLWithString:@"https://gdata.youtube.com/action/GetUploadToken"]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:[NSString stringWithFormat:@"GoogleLogin auth=\"%@\"", self.authorizationToken] forHTTPHeaderField:@"Authorization"]; 
    [request setValue:@"2" forHTTPHeaderField:@"GData-Version"]; 
    [request setValue:@"unlisted" forHTTPHeaderField:@"privacyStatus"]; 
    [request setValue:[NSString stringWithFormat:@"key=%@", self.developerKey] forHTTPHeaderField:@"X-GData-Key"]; 
    [request setValue:@"application/atom+xml; charset=UTF-8" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:[NSString stringWithFormat:@"%u", (unsigned int)xml.length] forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]]; 

    self.responseData   = [[NSMutableData alloc] init]; 
    self.currentConnection = [DDURLConnection connectionWithRequest:request delegate:self]; 
    [self.currentConnection setType:DDYouTubeUploaderConnectionTypePrepare]; 

    // Create error if there were 
    // problems creating a connection 
    if (!self.currentConnection) 
    { 
     *error = [self createErrorWithCode:DDYouTubeUploaderErrorCodeCannotCreateConnection 
           description:@"Cannot create connection to YouTube."]; 
    } 
} 

- (BOOL)uploadVideoFile:(NSURL *)fileURL 
        error:(NSError **)error 
{ 
    NSString *boundary = @"AbyRvAlG"; 
    NSString *nextURL = @"http://www.youtube.com"; 

    NSData *fileData = [NSData dataWithContentsOfFile:[fileURL relativePath]]; 
    _videoFileLength = [fileData length]; 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?nexturl=%@", self.uploadURLString, nextURL]]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"]; 

    NSMutableData *body   = [NSMutableData data]; 
    NSMutableString *bodyString = [NSMutableString new]; 

    // Add token 
    [bodyString appendFormat:@"\r\n--%@\r\n", boundary]; 
    [bodyString appendString:@"Content-Disposition: form-data; name=\"token\"\r\n"]; 
    [bodyString appendString:@"Content-Type: text/plain\r\n\r\n"]; 
    [bodyString appendFormat:@"%@", self.uploadToken]; 

    // Add file name 
    [bodyString appendFormat:@"\r\n--%@\r\n", boundary]; 
    [bodyString appendFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", [fileURL lastPathComponent]]; 
    [bodyString appendFormat:@"Content-Type: application/octet-stream\r\n\r\n"]; 
    [bodyString appendFormat:@"privacyStatus: unlisted\r\n\r\n"]; 

    // Create the data 
    [body appendData:[bodyString dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:fileData]]; 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // Set the body 
    [request setHTTPBody:body]; 

    // Create the connection 
    self.responseData   = [[NSMutableData alloc] init]; 
    self.currentConnection = [DDURLConnection connectionWithRequest:request delegate:self]; 
    [self.currentConnection setType:DDYouTubeUploaderConnectionTypeUpload]; 

    if (!self.currentConnection) 
    { 
     *error = [self createErrorWithCode:DDYouTubeUploaderErrorCodeCannotCreateConnection 
           description:@"Cannot create connection to YouTube."]; 
     return NO; 
    } 

    return YES; 
} 

これは完全に働いて、 しかし、問題はPublicとしてアップロードされた動画で、私はそれをアップロードしますUnlistedとなります。
私は非常に多くのタグを試しましたが、成功することはできませんでした。
使用さ 、
- プライバシー
- privacystatus

、誰もが私はタグといただきましたタグを追加する必要がどこに私が知っていることはできますか?
コードスニペットが役立ちます。

答えて

2

だけ

<yt.accesscontrol> 

を追加することによって、XMLを更新し、それが非上場

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>" 
       @"<yt:accessControl action='list' permission='denied'/>" 
       @"</entry>", title, desc, category, keywords]; 
としてビデオをuplaoadます
関連する問題