12

I'm writing an application that call google fit rest api from Flutter.

I need to sign with google using (https://pub.dev/packages/google_sign_in). I can obtain a token without problem (see Did anyone manage to get the id token from google sign in (Flutter)) but how to obtain a new token when it is expired?

I dont' want to ask to the user to login and obtain a new token every hour

Alessandro Verona
  • 1,157
  • 9
  • 23

2 Answers2

8

You can do this in 2 ways.

  1. You can use the API.

  2. I don't know if this is standard but you can do a silent login every time the user opens the app, the silent log logs into the user account without user interaction and this way you have a new token. Like this:

Future<String> refreshToken() async {
    print("Token Refresh");
    final GoogleSignInAccount googleSignInAccount =
        await googleSignIn.signInSilently();
    final GoogleSignInAuthentication googleSignInAuthentication =
        await googleSignInAccount.authentication;

    final AuthCredential credential = GoogleAuthProvider.getCredential(
      accessToken: googleSignInAuthentication.accessToken,
      idToken: googleSignInAuthentication.idToken,
    );
    final AuthResult authResult = await auth.signInWithCredential(credential);

    return googleSignInAuthentication.accessToken; // New refreshed token
  }
JideGuru
  • 7,102
  • 6
  • 26
  • 48
  • Hi, using the api seem to be impossible because my token come from a Android client id and I doesn't have a "secret" key. The second options is something I hadn't thought of and is very promising and seem to works, thanks – Alessandro Verona Jun 06 '20 at 12:10
  • Oh yes for sure, I just tried and seem to works, thanks ;) – Alessandro Verona Jun 06 '20 at 12:30
  • It would be nice if you can mention an article where it explains how to update it via API. For me, the second method is giving the same token! Maybe I need to wait for an hour before testing it. – Shajeel Afzal Jul 12 '20 at 00:06
  • You are AMAZING! I've been working on this problem for so long and your solution was so simple and easy to implement. Thanks! – Hudson Kim Aug 17 '20 at 22:18
  • 1
    @HudsonKim Happy to help – JideGuru Aug 17 '20 at 23:01
  • 1
    this solution is not working and i'am always getting the same old token ... – user1485365 Dec 08 '20 at 22:45
  • Solution 2 doesn't work for me on android (works for iOS). It gives the same token again. I have posted code snippet from the plug in codes on plugins repo issue (https://github.com/googlesamples/google-signin-unity/issues/24#issuecomment-757544151) and seems to be doing it by design, is there any workaround? – Vijay Feb 02 '21 at 01:45
  • An `access token` is not the same as `refresh token`, Currently, The official google_sign_in plugin does not support returning refresh token https://github.com/flutter/flutter/issues/45847 – Mahesh Jamdade Sep 22 '21 at 13:22
0

Before calling '.signIn()' method make sure that GoogleSignInAccount variable type has 'forceCodeForRefreshToken' set up with true.

After that take 'serverAuthCode' and make a request to https://accounts.google.com/o/oauth2/token with the next body

'access_type': 'offline', tokenType: <serverAuthCode>, 'grant_type': 'code', 'client_secret': <yourClientSecret>, 'client_id': <yourClientId>, 'redirect_uri': <yourMiddleWare>,.

In response you will get that refresh_token.

OpTiM B
  • 45
  • 10