refreshCredentials method

Future<Client> refreshCredentials([
  1. List<String>? newScopes
])

Explicitly refreshes this client's credentials. Returns this client.

This will throw a StateError if the Credentials can't be refreshed, an AuthorizationException if refreshing the credentials fails, or a FormatException if the authorization server returns invalid responses.

You may request different scopes than the default by passing in newScopes. These must be a subset of the scopes in the Credentials.scopes field of Client.credentials.

Implementation

Future<Client> refreshCredentials([List<String>? newScopes]) async {
  if (!credentials.canRefresh) {
    var prefix = 'OAuth credentials';
    if (credentials.isExpired) prefix = '$prefix have expired and';
    throw StateError("$prefix can't be refreshed.");
  }

  // To make sure that only one refresh happens when credentials are expired
  // we track it using the [_refreshingFuture]. And also make sure that the
  // _onCredentialsRefreshed callback is only called once.
  if (_refreshingFuture == null) {
    try {
      _refreshingFuture = credentials.refresh(
        identifier: identifier,
        secret: secret,
        newScopes: newScopes,
        basicAuth: _basicAuth,
        httpClient: _httpClient,
      );
      _credentials = await _refreshingFuture!;
      _onCredentialsRefreshed?.call(_credentials);
    } finally {
      _refreshingFuture = null;
    }
  } else {
    await _refreshingFuture;
  }

  return this;
}