clientViaRefreshToken function

Future<AutoRefreshingAuthClient> clientViaRefreshToken(
  1. ClientId clientId,
  2. String refreshToken,
  3. List<String> scopes, {
  4. Client? baseClient,
  5. AuthEndpoints authEndpoints = const GoogleAuthEndpoints(),
  6. String? quotaProject,
})

Creates an AutoRefreshingAuthClient from a refresh token.

The clientId that you obtain from the API Console Credentials page, as described in Obtain OAuth 2.0 credentials.

The refreshToken is used to obtain and automatically refresh access tokens for the given scopes.

If baseClient is provided, all HTTP requests will be made with it. Otherwise, a new Client instance will be created.

The user is responsible for closing the returned HTTP Client. Closing the returned Client will not close baseClient.

If quotaProject is provided, it will be added to the X-Goog-User-Project header for all requests.

Implementation

Future<AutoRefreshingAuthClient> clientViaRefreshToken(
  ClientId clientId,
  String refreshToken,
  List<String> scopes, {
  Client? baseClient,
  AuthEndpoints authEndpoints = const GoogleAuthEndpoints(),
  String? quotaProject,
}) async {
  final setupClient = setupBaseClient(baseClient);
  final closeUnderlyingClient = baseClient == null;

  try {
    final credentials = await refreshCredentials(
      clientId,
      AccessCredentials(
        // Deliberately expired — forces a token exchange immediately.
        AccessToken(
          'Bearer',
          '',
          DateTime.fromMillisecondsSinceEpoch(0, isUtc: true),
        ),
        refreshToken,
        scopes,
      ),
      setupClient,
      authEndpoints: authEndpoints,
    );

    return AutoRefreshingClient(
      setupClient,
      authEndpoints,
      clientId,
      credentials,
      closeUnderlyingClient: closeUnderlyingClient,
      quotaProject: quotaProject,
    );
  } catch (e) {
    setupClient.close();
    rethrow;
  }
}