Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain a new token by refresh token with google_sign_in Flutter

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

like image 386
Alessandro Verona Avatar asked Jun 06 '20 10:06

Alessandro Verona


People also ask

What is an OAuth2 refresh token?

Get an OAuth2 Refresh Token and Configure Your Client Because OAuth2 access expires after a limited time, an OAuth2 refresh tokenis used to automatically renew OAuth2 access.

What are the basic concepts of refrefresh token?

Refresh token First call Basic concepts Overview API structure Entity relationships Versioning Changing and inspecting objects Retrieving objects Resource metadata Call structure Mutates Overview Resource mutates

How many times can I use a refresh token?

once you generate a new token using the refresh token, that refresh token will not work more. You have to use new access and refresh token you got. That refresh token can be used to get new access and refresh token again. your refresh token can be used only once to get another set of tokens. Every time you use the new one.

What are the third party APIs for OAuth2?

Third-Party APIs Apps Scripts execution DoubleClick Search Generate OAuth2.0 refresh token Natural Language OAuth1.0 library OAuth2.0 library Plivo Slack Sportradar Twilio Twitter (OAuth1.0) Twitter (OAuth2.0) Home Products Google Ads scripts Samples Send feedback Generate OAuth2.0 refresh token


Video Answer


1 Answers

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
  }
like image 134
JideGuru Avatar answered Oct 21 '22 03:10

JideGuru