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);
});
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.