如何在Youtube中上传未列出隐私设置的视频

时间:2016-08-10 14:54:11

标签: ios youtube

我正在使用此代码在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

任何人都可以让我知道我应该在哪里添加标签并标记标签? 代码段会更有帮助。

1 个答案:

答案 0 :(得分:2)

只需添加

即可更新xml
<yt.accesscontrol>

它会将视频上传为不公开的视频

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