使用GTLRYouTubeService上传视频到谷歌失败

时间:2018-04-01 14:30:22

标签: ios xcode youtube google-api

我正在尝试使用我的iOS应用中的GTLRYouTubeService将视频上传到youtube

登录过程是通过GIDSignIn完成的,到目前为止一直有效。

当用户登录时,我正在设置youtube-service:

self.youTubeService = [GTLRYouTubeService new];
self.youTubeService.APIKey = API_KEY;
self.youTubeService.authorizer = user.authentication.fetcherAuthorizer;

然后我正在准备这样的视频对象:

GTLRYouTube_VideoStatus *status = [GTLRYouTube_VideoStatus object];
status.privacyStatus = @"private";

GTLRYouTube_VideoSnippet *snippet = [GTLRYouTube_VideoSnippet object];
snippet.title = self.mTitleField;
snippet.descriptionProperty = self.mDescriptionField;
snippet.tags = [self.mKeywordsField componentsSeparatedByString:@","];

GTLRYouTube_Video *video = [GTLRYouTube_Video object];
video.status = status;
video.snippet = snippet;

之后,我初始化上传:

GTLRUploadParameters *uploadParameters =
    [GTLRUploadParameters uploadParametersWithFileURL:fileToUploadURL
                                             MIMEType:@"video/mp4"];
    uploadParameters.uploadLocationURL = locationURL;

    GTLRYouTubeQuery_VideosInsert *query =
    [GTLRYouTubeQuery_VideosInsert queryWithObject:video
                                              part:@"snippet,status"
                                  uploadParameters:uploadParameters];

    [self.mProgressView setProgress: 0.0];

    query.executionParameters.uploadProgressBlock = ^(GTLRServiceTicket *ticket,
                                                      unsigned long long numberOfBytesRead,
                                                      unsigned long long dataLength) {
        NSLog(@"Bytes read %f", (double)numberOfBytesRead);
        [self.mProgressView setProgress: (1.0 / (double)dataLength) * (double)numberOfBytesRead];
    };

    NSLog(@"YT-Service %@", self.youTubeService.authorizer); // Returns not null

    _uploadFileTicket = [self.youTubeService executeQuery:query
                            completionHandler:^(GTLRServiceTicket *callbackTicket,
                                                GTLRYouTube_Video *uploadedVideo,
                                                NSError *callbackError) {
                                // Callback
                                _uploadFileTicket = nil;
                                if (callbackError == nil) {

                                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Uploaded" message:@"Your video has been uploaded to YouTube" preferredStyle:UIAlertControllerStyleAlert];
                                    UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                                    [alert addAction:okButton];
                                    [self presentViewController:alert animated:YES completion:nil];

                                } else {

                                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Upload failed" message:[NSString stringWithFormat:@"%@", callbackError.localizedDescription] preferredStyle:UIAlertControllerStyleAlert];
                                    UIAlertAction *okButton = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                                    [alert addAction:okButton];
                                    [self presentViewController:alert animated:YES completion:nil];

                                }

                                self.isUploading = NO;
                                [self.mProgressView setProgress: 0.0];

                            }];

但该服务返回403(拒绝访问)错误。

有什么想法在这里发生了什么?

[编辑]

经过进一步调查后,我发现该服务返回“权限不足” - 但我的帐户设置正确。通过YouTube页面上传视频。

1 个答案:

答案 0 :(得分:0)

嗯,答案很简单,实际上......

我必须将以下内容添加到共享的GIDSignIn实例中:

[GIDSignIn sharedInstance].scopes = @[kGTLRAuthScopeYouTube];

如果没有这个,登录不会要求将视频上传到YouTube的范围,因此,您没有权利这样做(如果在某处提到,那就太棒了在文档中)

接下来的问题是,一旦授予访问权限,我就会收到此错误:

{"domain":"usageLimits",
"reason":"ipRefererBlocked",
"message":"The request did not specify any iOS bundle ID. Please ensure that the client is sending it or use the API Console to update your key restrictions."...}

同样,这出现了403错误。

事实证明,我已经为我的API-Key设置了应用程序限制。但是当GDSignIn与GTLR-API一起使用时,它们不起作用。所以我不得不删除限制。此外,文档中没有提到......

现在,一切正常......