diff --git a/geemap/common.py b/geemap/common.py index 17a66b5ae9..b01f161ce2 100644 --- a/geemap/common.py +++ b/geemap/common.py @@ -44,6 +44,7 @@ def ee_initialize( Args: token_name (str, optional): The name of the Earth Engine token. Defaults to "EARTHENGINE_TOKEN". + In Colab, you can also set a secret named "EE_PROJECT_ID" to initialize Earth Engine. auth_mode (str, optional): The authentication mode, can be one of colab, notebook, localhost, or gcloud. See https://developers.google.com/earth-engine/guides/auth for more details. Defaults to None. service_account (bool, optional): If True, use a service account. Defaults to False. @@ -61,9 +62,16 @@ def ee_initialize( if auth_mode is None: if in_colab_shell(): - auth_mode = "notebook" + from google.colab import userdata + + try: + project_id = userdata.get("EE_PROJECT_ID") + auth_mode = "colab" + kwargs["project"] = project_id + except Exception: + auth_mode = "notebook" else: - auth_mode = "localhost" + auth_mode = "notebook" auth_args["auth_mode"] = auth_mode @@ -122,16 +130,6 @@ def ee_initialize( ) # deals with token generated by old auth method. with open(credential_file_path, "w") as f: f.write(credential) - # elif in_colab_shell(): - # if credentials_in_drive() and (not credentials_in_colab()): - # copy_credentials_to_colab() - # elif not credentials_in_colab: - # ee.Authenticate(**auth_args) - # if is_drive_mounted() and (not credentials_in_drive()): - # copy_credentials_to_drive() - # else: - # if is_drive_mounted(): - # copy_credentials_to_drive() ee.Initialize(**kwargs)