youtube 上传错误:GTLJSONRPCErrorDomain Code=401



当我尝试登录YouTube并上传视频时,它上传没有任何问题。如果我在 2-3 小时后上传视频,我会收到一条错误消息,说:

Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 "The operation couldn’t be completed. (Invalid Credentials)" UserInfo=0x14585d90 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x14d85ba0: {message:"Invalid Credentials" code:401 data:[1]}, NSLocalizedFailureReason=(Invalid Credentials)}

这是执行Youtube登录的代码,

GIDSignIn *googleSignIn = [GDSharedInstance googleSDKApplicationSharedInstance];
    googleSignIn.delegate = self;
    googleSignIn.scopes  = [NSArray arrayWithObject:@"https://www.googleapis.com/auth/youtube"];
    [googleSignIn signIn];

登录委托

  - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error
    { 
// Auth is converted to use it for uploading a video
     GTMOAuth2Authentication *youTubeAuth = [[GTMOAuth2Authentication alloc] init];
        youTubeAuth.clientID = kClientID;
        youTubeAuth.clientSecret = @"xxx";
        youTubeAuth.userEmail = googleUser.profile.email;
        youTubeAuth.userID = googleUser.userID;
        youTubeAuth.accessToken = googleUser.authentication.accessToken;
        youTubeAuth.refreshToken = googleUser.authentication.refreshToken;
        youTubeAuth.expirationDate = googleUser.authentication.accessTokenExpirationDate;
        self.youTubeService.authorizer = youTubeAuth;
    }

上传代码,

NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:[NSURL URLWithString:path] error:&error];
    if (fileHandle) {
        NSString *mimeType = [self MIMETypeForFilename:filename
                                       defaultMIMEType:@"video/mov"];
        GTLUploadParameters *uploadParameters =
        [GTLUploadParameters uploadParametersWithFileHandle:fileHandle
                                                   MIMEType:mimeType];
        uploadParameters.uploadLocationURL = locationURL;
        GTLQueryYouTube *query = [GTLQueryYouTube queryForVideosInsertWithObject:video
                                                                            part:@"snippet,status,recordingDetails"
                                                                uploadParameters:uploadParameters];
            GTLServiceYouTube *service = self.youTubeService;
            self.uploadFileTicket = [service executeQuery:query
                                    completionHandler:^(GTLServiceTicket *ticket,
                                                        GTLYouTubeVideo *uploadedVideo,
                                                        NSError *error)
                                     {
                                        // here I will get 401 error
                                    }];
     }

唯一的问题是GTLServiceYouTube。GIDSignIn 似乎处理刷新令牌,以便用户在首次登录后始终登录。但是GTLOAuth2身份验证仅在第一次登录时有效,并在一小时后被破坏。

令牌需要刷新。

使用这段代码:-

- (void)applicationWillEnterForeground:(UIApplication *)application {
 [[GIDSignIn sharedInstance] signInSilently]
}

这里的问题是您的身份验证令牌即将过期。 在旧令牌过期后,必须使用刷新令牌获取新的有效身份验证令牌。

如果您使用的是旧版 Google Plus iOS SDK,则可以使用 GTMOAuth2Authentication 通过 authorizeRequest: 方法强制刷新身份验证令牌。

GTMOAuth2Authentication.h

// The request argument may be nil to just force a refresh of the access token,
// if needed.
- (void)authorizeRequest:(NSMutableURLRequest *)request
       completionHandler:(void (^)(NSError *error))handler;

实现:

// In your sign in method
[[GPPSignIn sharedInstance] setKeychainName:@"googleAuth"];
// ...
// Retrieving auth and refreshing token
GTMOAuth2Authentication *auth;
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:@"googleAuth"
                                                             clientID:@"kYourGoogleClientId"
                                                         clientSecret:@"kYourGoogleClientSecret"];
NSLog(@"old auth: %@", auth);
[auth authorizeRequest:nil completionHandler:^(NSError *error) {
    if (error) {
        // no auth data or refresh failed
        NSLog(@"Error: %@", error);
    } else {
        // Auth token refresh successful
        NSLog(@"new auth: %@", auth);
    }
}];

相关内容

  • 没有找到相关文章