Google Plus iOS SDK not refreshing expired token

2019-09-11 08:00发布

问题:

I have a problem, which might have a so simple solution, that I am not able to see it.

I installed the Google Plus iOS SDK into my app, and got my app running with Google very nice.

Google Plus iOS SDK

BUT, after one hour, the token is expiring, and I don't find a way to renew it.

- (void)renewGoogleToken {
    GPPSignIn *gppSignIn                        = [GPPSignIn sharedInstance];
    gppSignIn.shouldFetchGooglePlusUser         = YES;
    gppSignIn.shouldFetchGoogleUserEmail        = YES;
    gppSignIn.clientID                          = [DSUtils getGoogleClientID];
    gppSignIn.scopes                            = @[@"https://www.googleapis.com/auth/plus.login",
                                                @"https://www.googleapis.com/auth/calendar"];
    gppSignIn.delegate                          = self;

    BOOL success                                = [gppSignIn trySilentAuthentication];  }

I call this method initially, when I want to login with Google+. After one hour, when token is about to expire, I call this method again, but I receive same token, which is about to expire.

Anybody can help me please? I searched intensely on google, and could not find a solution. The SDK Docs doesn't say anything about renewing, which is strange. Is token refreshed automatically?

Please help, Thanks.

回答1:

The object you get back is a GTMOAuth2Authentication. If being used in a regular Google service request, it should automatically be refreshed automatically if needed. However, if you need to force a refresh you can request authorization of a nil request:

[auth authorizeRequest:nil
          delegate:self     
 didFinishSelector:@selector(authentication:request:finishedWithError:)];

Or with blocks

[auth authorizeRequest:nil 
 completionHandler:^(NSError *error) { //...
}];