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
hjortgaard
source share