加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – iOS YouTube视频上传错误

发布时间:2020-12-16 06:59:10 所属栏目:百科 来源:网络整理
导读:我正在尝试使用Objective-C和YouTube API上传视频,但它无法正常工作并在最后一步返回错误.该错误显示“需要用户身份验证”. 我正在关注这个API document,特别是没有元数据的那个.我获得了ClientLogin API的身份验证令牌 我用NSLog检查了身份验证令牌,它就在
我正在尝试使用Objective-C和YouTube API上传视频,但它无法正常工作并在最后一步返回错误.该错误显示“需要用户身份验证”.

我正在关注这个API document,特别是没有元数据的那个.我获得了ClientLogin API的身份验证令牌

我用NSLog检查了身份验证令牌,它就在那里.我看到上传API也会返回上传网址,但是当我向检索到的上传网址发送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-8nn"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[xml dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Type: video/mp4n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Transfer-Encoding: binarynn"] 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;
    }
}

解决方法

我想你应该检查一下: –

https://developers.google.com/youtube/2.0/developers_guide_protocol_video_feeds

你也检查一下: –

http://urinieto.com/2010/10/upload-videos-to-youtube-with-iphone-custom-app/

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读