requestAuthorizationCode function
Obtains CodeResponse using the Google Identity Services code model.
See Choose a user authorization model to understand the tradeoffs between using this function and requestAccessCredentials.
See https://developers.google.com/identity/oauth2/web/guides/use-code-model and https://developers.google.com/identity/oauth2/web/reference/js-reference for more details.
Implementation
Future<CodeResponse> requestAuthorizationCode({
required String clientId,
required Iterable<String> scopes,
String? state,
String? hint,
String? hostedDomain,
@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<CodeResponse>();
void callback(gis.CodeResponse response) {
if (response.error != null) {
window.console.log(response);
completer.completeError(
AuthenticationException(
response.error!,
errorDescription: response.error_description,
errorUri: response.error_uri,
),
);
return;
}
completer.complete(CodeResponse._(
code: response.code!,
scopes: response.scope,
state: response.state,
));
}
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.CodeClientConfig(
callback: allowInterop(callback),
client_id: clientId,
scope: scopes.toList(),
state: state,
login_hint: hint,
hd: hostedDomain,
error_callback: allowInterop(errorCallback),
);
final client = gis.oauth2.initCodeClient(config);
client.requestCode();
return completer.future;
}