theboshy
theboshy

Reputation: 418

How to correctly refresh auth token in the background google-apis using flutter

Right now I use the method clientViaUserConsent, to authenticate my user and thus be able to use the Google API, in this case, calendar api, the problem is that the way it is built now requests this permission every time the user enters the app, this it is a problem since it is annoying for the user,

Is there a way to do this automatically by refreshing the authentication token in the background every time the user enters the app?

I understand the idea of ​​storing the token and the refresh token but I don't know how to implement it

here is my code:

var _clientID = new ClientId(Secret.getId(), "");
    const _scopes = const [cal.CalendarApi.calendarScope];
    await clientViaUserConsent(_clientID, _scopes, Prompt.prompt).then((AuthClient client) async {
      CalendarClient.calendar = cal.CalendarApi(client);
      print(client);
    });

Upvotes: 4

Views: 1489

Answers (2)

Rahul Raj
Rahul Raj

Reputation: 1493

Add following dependencies

  google_sign_in: ^4.4.6
  googleapis_auth: ^0.2.11+1
  googleapis: ^0.54.0

Follow the Methods

Initialize google SignIn clientId and scope

  final GoogleSignIn _googleSignIn = GoogleSignIn(
  clientId:
      'hghdf4876487984398',
  scopes: <String>[
    googleAPI.CalendarApi.CalendarScope,
  ]);

Call this method for when ever you want

    void initState() {
    getGoogleData();
    super.initState();
  }

Here the funtion

    Future getGoogleData() async {
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    final _accessToken= await googleUser.authentication;

    print(_accessToken.accessToken);
    try {
      final GoogleAPIClient httpClient =
          GoogleAPIClient(await googleUser.authHeaders);
      final googleAPI.CalendarApi calendarAPI =
          googleAPI.CalendarApi(httpClient);
    } catch (e) {
      refreshFn(googleUser);
//this funtion used for refresh token
    }
  }

Refresh Funtion

  refreshFn(googleUser) {
    googleUser.clearAuthCache();
    getGoogleEventsData().whenComplete(() => Navigator.pop(context));
  }

Upvotes: 0

Andy
Andy

Reputation: 81

Yes, the credentials are stored in the client variable.

AccessToken: client.credentials.accessToken.data
RefreshToken: client.credentials.refreshToken
Expiration: client.credentials.accessToken.expiry
IDToken: client.credentials.idToken

You can then store these and build a new request later when they sign in as such:

void getEventsPastAuth() async {
  var client = Client();
  AccessCredentials credentials = await _calendarCredentials.getCredetial();
  AccessCredentials refreshedCred =
      await refreshCredentials(_credentialsID, credentials, client);

  client = autoRefreshingClient(_credentialsID, credentials, client);
  var calendar = CalendarApi(client);
  var now = DateTime.now();
  var calEvents = calendar.events.list('primary',
      maxResults: 100,
      timeMin: now,
      timeMax: DateTime(now.year, now.month + 1, now.day));
  calEvents.then((Events events) {
    events.items!.forEach((Event event) {
      debugPrint(event.summary);
    });
  });
}

The variable credentials is the stored variables as an AccessCredential

AccessCredentials(
      AccessToken("Bearer", accessToken, DateTime.parse(expiration)),
      refreshToken,
      _scopes,
      idToken: idToken);

If you figure out how to have access to multiple user's calendars simultaneously, let me know. I'm currently struggling on figuring out how I can search multiple users' calendars after user consent in Flutter.

Upvotes: 1

Related Questions