clientViaUserConsent function
- ClientId clientId,
- List<
String> scopes, - PromptUserForConsent userPrompt, {
- Client? baseClient,
- String? hostedDomain,
- int listenPort = 0,
- String? customPostAuthPage,
- AuthEndpoints authEndpoints = const GoogleAuthEndpoints(),
Obtains oauth2 credentials and returns an authenticated HTTP client.
See obtainAccessCredentialsViaUserConsent for specifics about the arguments used for obtaining access credentials.
The clientId
that you obtain from the API Console
Credentials page,
as described in
Obtain OAuth 2.0 credentials.
HTTP requests made on the returned client will get an additional
Authorization
header with the AccessCredentials obtained.
Once the AccessCredentials expire, it will use it's refresh token
(if available) to obtain new credentials.
See autoRefreshingClient for more information.
If baseClient
is provided, all HTTP requests will be made with it.
Otherwise, a new Client
instance will be created.
If provided, restricts sign-in to Google Apps hosted accounts at
hostedDomain
. For more details, see
https://developers.google.com/identity/protocols/oauth2/openid-connect#hd-param
The user is responsible for closing the returned HTTP Client
.
Closing the returned Client
will not close baseClient
.
The localhost
port to use when listening for the redirect from a user
browser interaction. Defaults to 0
- which means the port is dynamic.
Generally you want to specify an explicit port so you can configure it on the Google Cloud console.
{@macro googleapis_auth_custom_post_auth_page}
If provided, default post authentication page is replaced with html code
specified in customPostAuthPage
variable.
Implementation
Future<AutoRefreshingAuthClient> clientViaUserConsent(
ClientId clientId,
List<String> scopes,
PromptUserForConsent userPrompt, {
Client? baseClient,
String? hostedDomain,
int listenPort = 0,
String? customPostAuthPage,
AuthEndpoints authEndpoints = const GoogleAuthEndpoints(),
}) async {
var closeUnderlyingClient = false;
if (baseClient == null) {
baseClient = Client();
closeUnderlyingClient = true;
}
final flow = AuthorizationCodeGrantServerFlow(
authEndpoints,
clientId,
scopes,
baseClient,
userPrompt,
hostedDomain: hostedDomain,
listenPort: listenPort,
customPostAuthPage: customPostAuthPage,
);
AccessCredentials credentials;
try {
credentials = await flow.run();
} catch (e) {
if (closeUnderlyingClient) {
baseClient.close();
}
rethrow;
}
return AutoRefreshingClient(
baseClient,
authEndpoints,
clientId,
credentials,
closeUnderlyingClient: closeUnderlyingClient,
);
}