Google Plus SDK not refreshing token (iOS)

余生颓废 提交于 2019-12-23 02:28:47

问题


Using Google Plus SDK for sign-in. Everything works great at first, but appears that the oauth token is expiring after about 1 hour. I am following the recommendations on Google's site and using

[signIn trySilentAuthentication] 

in viewDidAppear function.

I know the token is expired because I am using Oauth on my server and getting a 401 response.

Also worth noting that this is not a problem if the iPhone/iPad is left plugged in with the screen on. In this case the app works indefinitely without any issues.

The authentication fails on my Cloud Endpoint. I am initializing the service as below:

static GTLService *service = nil;

if (!service) {
    service = [[GTLServiceMyModel alloc] init];
    service.retryEnabled = YES;
    [service setAuthorizer: [GPPSignIn sharedInstance].authentication];
 }

Also if the app has an expired token the work around I have right now is to close the app by using the iOS app switcher and launch app again. The app will then work again for about an hour before token expires.

Has anyone ran into this issue? Any help would be GREATLY appreciated!


回答1:


My issue was where I was setting the authorizer for the service. This code block was only getting ran the first time my method was called. Coming from a Java background I was not savvy on static variables in methods. Now I know!! Just moved the setAuthorizer to outside the block and everything works.

static GTLService *service = nil;
if (!service) {
    service = [[GTLServiceMyModel alloc] init];
    service.retryEnabled = YES;
    [service setAuthorizer: [GPPSignIn sharedInstance].authentication];   
}



回答2:


Usually a 401 response after 1 hour indicates refreshing access token failing. One reason I encountered is that happens when you test the app with an account that has already been giving refresh token. The refresh token is issued only once so there is a chance that your subsequent token grant did not have the refresh token to be used after the 3600 sec token expiration since it was already given the first time you connected. Try https://accounts.google.com/IssuedAuthSubTokens and manually revoke the access to google+ from this app and reconnect.



来源:https://stackoverflow.com/questions/20123341/google-plus-sdk-not-refreshing-token-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!