How to get update token using GIDSignIn and GTMOAuth2Authentication in iOS?

I am writing an iOS application that uses Google GIDSignIn [1] to log in users and GTLServiceYoutube to fulfill requests to Youtube (upload videos and upload Youtube video resources).

This works well when the user first logs in, but after about one hour the access token expires and the user can no longer make requests using GTLServiceYoutube due to error 401 (invalid credentials).

I use the following code to install GTMOAuth2Authentication after a successful login:

- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { if (error == nil) { [self setAuthorizerForSignIn:signIn user:user]; } [super signIn:signIn didSignInForUser:user withError:error]; } - (void)setAuthorizerForSignIn:(GIDSignIn *)signIn user:(GIDGoogleUser *)user { GTMOAuth2Authentication *auth = [[GTMOAuth2Authentication alloc] init]; [auth setClientID:signIn.clientID]; [auth setClientSecret:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"GoogleClientSecret"]]; [auth setUserEmail:user.profile.email]; [auth setUserID:user.userID]; [auth setAccessToken:user.authentication.accessToken]; [auth setRefreshToken:user.authentication.refreshToken]; [auth setExpirationDate: user.authentication.accessTokenExpirationDate]; [[UserManager sharedInstance].youTubeService setAuthorizer:auth]; } 

where [[UserManager sharedInstance].youTubeService is an instance of GTLServiceYouTube.

The only problem with GTLServiceYouTube. GIDSignIn seems to handle update tokens, so the user always logs in after the first login. But GTLOAuth2Authentication works only at the first login and is interrupted after an hour.

So my question is: am I doing something wrong here? Or am I missing something to get the appropriate access token in GTMOAuth2Authentication after the update?

[1] https://developers.google.com/identity/sign-in/ios/api/interface_g_i_d_sign_in

+8
ios youtube-api google-plus
source share
3 answers

I believe that the right way to do this is to sign the user again when the application is open, or if the token needs to be updated. This can be done by calling [[GIDSignIn sharedInstance] signInSilently] , and then when it finishes signing in the keychain or data store update with your new authentication tokens.

+3
source share

From GoogleSignIn 2.1.0 , making a call to [GIDSignIn sharedInstance].signInSilently; updates the credentials stored in [GIDSignIn sharedInstance].currentUser.authentication .

Run pod update in your project to upgrade to 2.1.0 SDK if you use Cocoapods.

+2
source share

Using GTMOAuth2Authentication you can force the authentication token to be updated using the authorizeRequest: method.

From GTMOAuth2Authentication.h

 // The request argument may be nil to just force a refresh of the access token, // if needed. - (void)authorizeRequest:(NSMutableURLRequest *)request completionHandler:(void (^)(NSError *error))handler; 

Implementation:

 // In your sign in method [[GPPSignIn sharedInstance] setKeychainName:@"googleAuth"]; // ... // Retrieving auth and refreshing token GTMOAuth2Authentication *auth; auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:@"googleAuth" clientID:@"kYourGoogleClientId" clientSecret:@"kYourGoogleClientSecret"]; NSLog(@"old auth: %@", auth); [auth authorizeRequest:nil completionHandler:^(NSError *error) { if (error) { // no auth data or refresh failed NSLog(@"Error: %@", error); } else { // Auth token refresh successful NSLog(@"new auth: %@", auth); } }]; 
0
source share

All Articles