【问题标题】:Upload a video to google using GTLRYouTubeService fails使用 GTLRYouTubeService 将视频上传到谷歌失败
【发布时间】:2018-09-10 22:48:51
【问题描述】:

我正在尝试使用我的 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 页面上传视频是可行的。

【问题讨论】:

    标签: ios xcode youtube google-api


    【解决方案1】:

    嗯,其实答案很简单……

    我必须将以下内容添加到共享的 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 一起使用时,它们不起作用。所以我不得不取消限制。此外,文档中没有提到这...

    现在,一切正常...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-07-04
      • 1970-01-01
      • 2017-10-18
      • 2021-12-16
      • 1970-01-01
      • 2019-03-15
      • 2021-12-24
      相关资源
      最近更新 更多