diff --git a/geemap/common.py b/geemap/common.py index 21ad714456..5bc19c2ad5 100644 --- a/geemap/common.py +++ b/geemap/common.py @@ -3299,7 +3299,7 @@ def ee_to_xarray( f"Failed to access Earth Engine internal state for current project: {e}\n" "Please provide the 'project' parameter explicitly or ensure Earth Engine is properly initialized." ) - + # Use current_project if available, otherwise use provided project if current_project is not None: ee.Initialize(project=current_project, opt_url=opt_url) diff --git a/geemap/coreutils.py b/geemap/coreutils.py index e4b6147dea..aff0d8e25e 100644 --- a/geemap/coreutils.py +++ b/geemap/coreutils.py @@ -76,6 +76,20 @@ def ee_initialize( if ee.data._get_state().credentials is not None: return + if get_env_var("EE_SERVICE_ACCOUNT") is not None: + + key_data = get_env_var("EE_SERVICE_ACCOUNT") + + try: + email = json.loads(key_data)["client_email"] + except json.JSONDecodeError as e: + raise ValueError(f"Invalid JSON for key_data: {e}") + except KeyError: + raise ValueError("key_data JSON does not contain 'client_email'") + credentials = ee.ServiceAccountCredentials(email=email, key_data=key_data) + ee.Initialize(credentials) + return + ee_token = get_env_var(token_name) if ee_token is not None: