ios - 如何使用YouTube中未列出的“隐私”设置上传视频

标签 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

谁能告诉我应该在哪里添加标签以及标签是什么?
代码段将更有用。

最佳答案

只需通过添加

<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];
上传视频

关于ios - 如何使用YouTube中未列出的“隐私”设置上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38876890/

相关文章:

youtube - 将Google视频代码转换为视频标题

ios - 检查 Swift 中两个字符串表达式之间的相似性

ios - 按下按钮时出现 Xcode 奇怪错误

ios - 将上一个和下一个按钮添加到 UIPicker 的附件 View 中?

iphone - 密码文本字段的顺序验证

ios - 依赖于另一个 ios 框架项目的 podspec

android - 在 Android 应用程序中缓冲 youtube 视频

jquery - 如何在 youtube 订阅小部件上更改 CSS

linux - Shell 文件解析以自动上传 youtube

ios - 如何将嵌入式 youtube 视频旋转为横向模式