【问题标题】:youtube upload error: GTLJSONRPCErrorDomain Code=401youtube 上传错误:GTLJSONRPCErrorDomain Code=401
【发布时间】:2016-05-04 10:10:37
【问题描述】:

当我尝试登录 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
                                    }];

     }

【问题讨论】:

    标签: ios youtube-api


    【解决方案1】:

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

    如果您使用的是旧版本的 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);
        }
    }];
    

    【讨论】:

    • 最初我只使用 GTLServiceYouTube,它为登录和视频上传提供 GTMOAuth2Authentication。所以没有问题。出于某种原因,我不得不使用 GIDSignIn 登录。因此,要上传视频,我必须将 GIDSignIn 的身份验证转换为 GTMOAuth2Authentication。因此我得到了这个过期问题。我刚刚检查了 GIDSignIn 身份验证到期日期。我发现它在登录后一个小时内过期。所以我使用了@vizllx 解决方案,[[GIDSignIn sharedInstance] signInSilently]。
    【解决方案2】:

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

    令牌需要刷新。

    使用这段代码:-

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

    【讨论】:

    • 我猜你对 GTLOAuth2Authentication 和 GIDSignIn auth 感到困惑,如果我理解你的答案没有错的话。看起来 GTLOAuth2Authentication 似乎处理了刷新令牌,并且 GIDSignIn 身份验证在首次登录 1 小时后过期。
    • 是的,我评论过的作品。感谢您的提示。
    • 我可以上传两次,第三次给出同样的错误。第四次,第五次成功。因此随机显示相同的错误。
    猜你喜欢
    • 2013-05-31
    • 2017-08-11
    • 2012-11-23
    • 2013-05-31
    • 2019-06-18
    • 2017-08-07
    • 2012-03-26
    • 1970-01-01
    • 2018-02-04
    相关资源
    最近更新 更多