如何使用Google离线访问令牌在iOS的后台进行身份validation?
简洁版本:
我想使用Objective-C的Google OAuth2客户端为我创build一个有效的GTMOAuth2Authentication
对象,以便在我的应用程序中使用,并使用我从后端获得的脱机访问令牌。 我怎么能做到这一点?
我的情况:
我的应用使用Google Analytics(分析)只读范围来获取有关用户网站的一些数据。 我完成这个任务的方式是使用Google OAuth2客户端为Objective C提供的GTMOAuth2ViewControllerTouch
ViewController进行login。然后,它将为我提供有效的GTMOAuth2Authentication
对象,以便我可以通过Google API客户端查询Google Analytics。
现在,我们不希望为用户提供Google Analytics访问权限(有些没有Google帐户,一般而言,我们希望通过应用保持简单的信息)。 这意味着我们需要使用我们的帐户login(可以访问所有网站的Google Analytics数据)。 显然,我们不能给我们的用户我们的凭据,所以我们必须find一个解决scheme。
我的计划:
我认为这个问题可以通过从我们的后端(通过SSLencryption)请求我们的(离线访问)令牌string来解决,将其保存到用户的钥匙串中,并在应用程序中进一步使用它来查询分析。 然后我们让用户login我们的服务(这样我们可以确定用户有权访问哪个网站),并显示数据。
我的问题:
我到处search过,查看了Google的(非常简单的)文档,检查了GTMOAuth2Authentication
源代码,但似乎无法绕开这个问题。 在我看来,会有这样的解决scheme(因为我们在CMS中使用类似的方法让用户张贴到我们的Facebook墙),所以我在这里错过了什么?
当前的login码:
GTMOAuth2ViewControllerTouch *viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope clientID:kMyClientID clientSecret:kMyClientSecret keychainItemName:kKeychainItemName completionHandler: ^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) { if (error != nil) { // Authentication failed DebugLog(@"Failed!"); } else { // Authentication succeeded DebugLog(@"Success!"); // Update interface self.loginButton.hidden = YES; self.authentication = auth; } }];
我曾经尝试过:
我尝试手动创build一个GTMOAuth2Authentication
对象,并设置我自己的所有参数(范围,clientid,secretid,访问令牌,刷新令牌,callbackuri,令牌url等),但我得到返回401:login所需的错误,当我尝试使用查询的对象。 所以我想这不是做这件事的方法。
链接:
- Google OAuth2客户端
- Google API Objective-C客户端
谢谢阅读!
您不需要GTMOAuth2Authentication实例。 您需要创build自己的实现GTMFetcherAuthorizationProtocol协议的类,并将其指定为您的授权者。
MyAuth *myAuth = [[MyAuth alloc] initWithAccessToken:accessToken]; googleService.authorizer = myAuth;
这个类需要根据你已有的访问令牌授权请求。 这个类的代码应该类似于这个。
@interface MyAuth : NSObject<GTMFetcherAuthorizationProtocol> @property (copy, nonatomic) NSString *accessToken; @property (copy, readonly) NSString *userEmail; @end @implementation MyAuth - (void)authorizeRequest:(NSMutableURLRequest *)request delegate:(id)delegate didFinishSelector:(SEL)sel { if (request) { NSString *value = [NSString stringWithFormat:@"%s %@", GTM_OAUTH2_BEARER, self.accessToken]; [request setValue:value forHTTPHeaderField:@"Authorization"]; } NSMethodSignature *sig = [delegate methodSignatureForSelector:sel]; NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig]; [invocation setSelector:sel]; [invocation setTarget:delegate]; [invocation setArgument:(__bridge void *)(self) atIndex:2]; [invocation setArgument:&request atIndex:3]; [invocation invoke]; } - (void)authorizeRequest:(NSMutableURLRequest *)request completionHandler:(void (^)(NSError *error))handler { if (request) { NSString *value = [NSString stringWithFormat:@"%@ %@", @"Bearer", self.accessToken]; [request setValue:value forHTTPHeaderField:@"Authorization"]; } } - (BOOL)isAuthorizedRequest:(NSURLRequest *)request { return NO; } - (void)stopAuthorization { } - (void)stopAuthorizationForRequest:(NSURLRequest *)request { } - (BOOL)isAuthorizingRequest:(NSURLRequest *)request { return YES; }
希望能帮助到你。