gpt4 book ai didi

ios - GTMOAuth2Authentication 存储访问 token 时出现问题

转载 作者:行者123 更新时间:2023-11-29 01:32:20 24 4
gpt4 key购买 nike

我在使用 [self isAuthorized] 来确认我之前获得的访问 token 时遇到了问题。

每次我使用适用于 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;
}

认证完成后,没有报错,应正确保存access token

// 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 来确保我收到了访问 token ,它确实收到了。

-(BOOL)isAuthorized {

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


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

但是当我查看我是否被授权时,没有保存 token (oauthToken 为 NULL),我需要重新登录。

注意:它在 iOS 9 之前的过去工作。我不知道它是否相关。

谢谢文森特

最佳答案

你可能想尝试像 this 这样的东西验证登录:

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

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

关于ios - GTMOAuth2Authentication 存储访问 token 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com