Skip to content

GoogleCredentialsLocalProvider

autogen.tools.experimental.google.authentication.GoogleCredentialsLocalProvider #

GoogleCredentialsLocalProvider(client_secret_file, scopes, token_file=None, port=8080)

Bases: GoogleCredentialsProvider

A Google credentials provider that gets the credentials locally.

PARAMETER DESCRIPTION
client_secret_file

The path to the client secret file.

TYPE: str

scopes

The scopes to request.

TYPE: list[str]

token_file

Optional path to the token file. If not provided, the token will not be saved.

TYPE: str DEFAULT: None

port

The port from which to get the credentials.

TYPE: int DEFAULT: 8080

Source code in autogen/tools/experimental/google/authentication/credentials_local_provider.py
def __init__(
    self,
    client_secret_file: str,
    scopes: list[str],  # e.g. ['https://www.googleapis.com/auth/drive/readonly']
    token_file: Optional[str] = None,
    port: int = 8080,
) -> None:
    """A Google credentials provider that gets the credentials locally.

    Args:
        client_secret_file (str): The path to the client secret file.
        scopes (list[str]): The scopes to request.
        token_file (str): Optional path to the token file. If not provided, the token will not be saved.
        port (int): The port from which to get the credentials.
    """
    self.client_secret_file = client_secret_file
    self.scopes = scopes
    self.token_file = token_file
    self._port = port

client_secret_file instance-attribute #

client_secret_file = client_secret_file

scopes instance-attribute #

scopes = scopes

token_file instance-attribute #

token_file = token_file

host property #

host

Localhost is the default host.

port property #

port

The port from which to get the credentials.

get_credentials #

get_credentials()

Get the Google credentials.

Source code in autogen/tools/experimental/google/authentication/credentials_local_provider.py
@require_optional_import(
    [
        "google_auth_httplib2",
        "google_auth_oauthlib",
    ],
    "google-api",
)
def get_credentials(self) -> "Credentials":  # type: ignore[no-any-unimported]
    """Get the Google credentials."""
    creds = None
    if self.token_file and os.path.exists(self.token_file):
        creds = Credentials.from_authorized_user_file(self.token_file)  # type: ignore[no-untyped-call]

    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        creds = self._refresh_or_get_new_credentials(creds)

        if self.token_file:
            # Save the credentials for the next run
            with open(self.token_file, "w") as token:
                token.write(creds.to_json())  # type: ignore[no-untyped-call]

    return creds  # type: ignore[no-any-return]