getAccessToken method
Get a valid access token, refreshing if needed.
Implementation
Future<String> getAccessToken() async {
final account = await _ensureSessionRestored();
if (account == null) {
throw Exception('Not signed in to Google.');
}
final auth = await account.authentication;
final token = auth.accessToken;
if (token == null || token.isEmpty) {
// Force token refresh.
dev.log('Refreshing Google access token', name: 'GoogleAuth');
final freshAuth = await account.authentication;
final freshToken = freshAuth.accessToken;
if (freshToken == null || freshToken.isEmpty) {
throw Exception('Failed to get Google access token.');
}
return freshToken;
}
return token;
}