【问题标题】:GTMOAuth2Authentication problems with storing access token存储访问令牌的 GTMOAuth2Authentication 问题
【发布时间】:2015-10-28 02:43:54
【问题描述】:

我无法使用[self isAuthorized] 来确认我之前获得的访问令牌。

每次我使用适用于 iOS 的 Google Drive SDK 登录时:

// Creates the auth controller for authorizing access to Google Drive.
-(GTMOAuth2ViewControllerTouch *)createAuthController {

    GTMOAuth2ViewControllerTouch *authController;
    authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scopes
                                                                clientID:kClientID
                                                            clientSecret:kClientSecret
                                                        keychainItemName:kKeychainItemName
                                                                delegate:self
                                                        finishedSelector:@selector(viewController:finishedWithAuth:error:)];
    return authController;
}

认证完成后,没有错误,应该正确保存访问令牌

// Handle completion of the authorization process, and updates the Drive service
// with the new credentials.
-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error {
    if (error != nil)
    {
        //[self showAlert:@"Authentication Error" message:error.localizedDescription];
        self.driveService.authorizer = nil;
    }
    else
    {
        self.driveService.authorizer = authResult;
    }
}

我使用 NSLog 来确保我收到了访问令牌并且确实收到了。

-(BOOL)isAuthorized {

    NSString *oauthToken = [((GTMOAuth2Authentication *)self.driveService.authorizer) accessToken];
    NSLog(@"oauthToken: %@", oauthToken);


    return [((GTMOAuth2Authentication *)self.driveService.authorizer) canAuthorize];
}

但是当我查看我是否被授权时,没有保存令牌(oauthToken 为 NULL),我需要再次登录。

N.B:在 iOS 9 之前它在过去工作。我不知道它是否相关。

谢谢 文森特

【问题讨论】:

    标签: ios google-drive-api


    【解决方案1】:

    您可能想尝试this 之类的方法来验证登录:

    // Check for authorization.
    GTMOAuth2Authentication *auth =
    [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                          clientID:kClientId
                                                      clientSecret:kClientSecret];
    if ([auth canAuthorize]) {
        [self isAuthorizedWithAuthentication:auth];
    }
    

    另一方面,如果您真的想要访问令牌,请查看此 SO post,但是,存储访问令牌并不是最佳做法,因为访问令牌有过期时间。祝你好运,希望这会有帮助。

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2012-07-18
      • 2016-04-02
      • 2021-07-16
      • 2021-09-13
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2011-09-06
      相关资源
      最近更新 更多