requestAccessCredentials function
Obtains AccessCredentials using the Google Identity Services token model.
The returned AccessCredentials will always have a null
value for
AccessCredentials.refreshToken and
AccessCredentials.idToken.
See Choose a user authorization model to understand the tradeoffs between using this function and requestAuthorizationCode.
See https://developers.google.com/identity/oauth2/web/guides/use-token-model and https://developers.google.com/identity/oauth2/web/reference/js-reference for more details.
Implementation
Future<AccessCredentials> requestAccessCredentials({
required String clientId,
required Iterable<String> scopes,
String prompt = 'select_account',
@Deprecated('Undocumented feature. Do not include in production code.')
String? logLevel,
}) async {
await gis_loader.loadWebSdk();
if (logLevel != null) _googleAccountsId.callMethod('setLogLevel', [logLevel]);
final completer = Completer<AccessCredentials>();
void callback(gis.TokenResponse response) {
if (response.error != null) {
window.console.log(response);
completer.completeError(
AuthenticationException(
response.error!,
errorDescription: response.error_description,
errorUri: response.error_uri,
),
);
return;
}
final token = AccessToken(
response.token_type!,
response.access_token!,
expiryDate(response.expires_in!),
);
final creds = AccessCredentials(token, null, response.scope);
completer.complete(creds);
}
void errorCallback(gis.GoogleIdentityServicesError? error) {
if (error != null) {
completer.completeError(
AuthenticationException(
error.type.toString(),
errorDescription: error.message,
errorUri:
'https://developers.google.com/identity/oauth2/web/reference/js-reference#TokenClientConfig',
),
);
}
}
final config = gis.TokenClientConfig(
callback: allowInterop(callback),
client_id: clientId,
scope: scopes.toList(),
prompt: prompt,
error_callback: allowInterop(errorCallback),
);
final client = gis.oauth2.initTokenClient(config);
client.requestAccessToken();
return completer.future;
}