在使用iPhone应用程序时,我正在使用Google Plus SDK(GPPSignIn)来获取和刷新身份验证令牌。
这些令牌持续1小时。当我使用过期的令牌发出请求时,我正在使用的Web服务返回特定的http响应代码,以便我知道我的令牌已过期。
收到我的令牌已过期的响应后,我进行呼叫以刷新身份验证令牌。现在,我想用新令牌重复对Web服务的请求。我想在我的服务器请求对象中完成所有这些操作。
如果我自己编写整个程序,则可以只将令牌刷新请求与Web服务器请求内联,当令牌请求完成时,我可以继续进行Web服务器请求。但是,由于令牌工作是通过Google Plus SDK完成的,并且他们使用委托模型来传递身份验证请求的完成情况,因此我不知道如何在刷新令牌之前将请求保留到Web服务。
目前,我正在尝试一个“非常糟糕”的想法,如下所示,在该想法中,我尝试将请求放入新线程,然后将该线程休眠一会儿,这时它会唤醒,我们希望新令牌可用。但是必须有更好的方法!
if (error !=nil && error.code == NSURLErrorUserCancelledAuthentication) {
//special handling of 401, which may be a Google Auth token failure.
// if it is, then let's go get a new token.
User *client = [[User alloc] init];
if(client.email != nil && client.password != nil){
if (client.accountType == AuthTypeGoogle) {
NSLog(@"attempt to get a new token");
// request new token
[client silentGoogleAuthentication];
//repeat request
if (repeatableRequest) {
dispatch_async(repeatQueue, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"sleepytime");
[self assignCredentialstoRequest];
[self makeRequest:notificationName repeatableRequest:false];
});
}
}
}
}
任何指针将不胜感激。我查看了许多iOS令牌刷新凭单,并且看到了很多过时的问题,但是没有一个可行的解决方案。
因此,在考虑了这个问题之后,我意识到了两件事。
因此,有了这两条信息,我就可以使用[clientsilentGoogleAuthentication]客户端了;调用捕获trySilentAuthentication调用的结果,然后将结果分配到代码中的正确位置,然后向对[clientsilentGoogleAuthentication]进行初始调用的函数返回一个布尔值,然后继续进行。像这样:
// Try signing in silently so user doesn't have to be prompted again
BOOL authResponse = [signIn trySilentAuthentication];
if (authResponse) {
NSLog(@"successful auth");
NSLog(@"idToken is %@", [GPPSignIn sharedInstance].idToken);
self.accountType = AuthTypeGoogle;
self.password = [GPPSignIn sharedInstance].idToken;
[self updateCredentials];
return true;
} else {
return false;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句