Upload a video to google using GTLRYouTubeService fails

北城余情 提交于 2021-02-20 03:59:49

问题


I'm trying to upload a video to youtube using GTLRYouTubeService from my iOS-app

The login process is done via GIDSignIn and works so far.

When the user logged in, I'm setting up the youtube-service:

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

Then I'm preparing the video object like this:

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;

After that, I initialize the upload:

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

                            }];

But the service returns a 403 (Access denied) error.

Any ideas what's going on here?

[EDIT]

After further investigation, I saw that the service returns «insufficient permissions» - but my account is set up correctly. Uploading videos via YouTube page works.


回答1:


Well, the answer was pretty simple, actually...

I had to add the following to the shared GIDSignIn-instance:

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

Without that, the scopes for uploading videos to YouTube aren't requested by the sign in and thus, you get no rights for doing that (it'd be awesome if that would be mentioned somewhere in the docs)

Next problem was, that once the access was granted, I received this error:

{"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."...}

Again, this came with a 403-error.

Turns out, I had set up the application restrictions for my API-Key. But they don't work when using GDSignIn with the GTLR-API. So I had to remove the restrictions. Also, this is nowhere mentioned in the docs...

Now, everything's working fine...



来源:https://stackoverflow.com/questions/49599083/upload-a-video-to-google-using-gtlryoutubeservice-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!