def prepare_auth_credentials(
      self,
  ) -> AuthPreparationResult:
    """Prepares authentication credentials, handling exchange and user interaction."""

    # no auth is needed
    if not self.auth_scheme:
      return AuthPreparationResult(state="done")

    # Check for existing credential.
    existing_result = self._handle_existing_credential()
    if existing_result:
      return existing_result

    # fetch credential from adk framework
    # Some auth scheme like OAuth2 AuthCode & OpenIDConnect may require
    # multi-step exchange:
    # client_id , client_secret -> auth_uri -> auth_code -> access_token
    # -> bearer token
    # adk framework supports exchange access_token already
    fetched_credential = self._get_auth_response() or self.auth_credential

    exchanged_credential = self._exchange_credential(fetched_credential)

    if exchanged_credential:
      self._store_credential(exchanged_credential)
      return AuthPreparationResult(
          state="done",
          auth_scheme=self.auth_scheme,
          auth_credential=exchanged_credential,
      )
    else:
      self._request_credential()
      return AuthPreparationResult(
          state="pending",
          auth_scheme=self.auth_scheme,
          auth_credential=self.auth_credential,
      )
Prepares authentication credentials, handling exchange and user interaction.
src/google/adk/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py