Authentication overview

Google Earth Engine (GEE) provides a platform for planetary-scale geospatial analysis, requiring robust authentication mechanisms to secure access to its vast data catalog and computational resources. The platform integrates with Google Cloud's identity and access management system, leveraging OAuth 2.0 as its primary authentication framework. This standard protocol is designed to grant delegated access to protected resources without sharing user credentials directly with client applications.

For individuals and non-commercial research, GEE offers free access, typically authenticating via a Google user account. Commercial use cases, however, integrate with Google Cloud Platform (GCP) and require a GCP project, which enables granular control over permissions and resource usage. Authentication processes vary slightly depending on whether you are using the GEE Code Editor (JavaScript API) or the Python API, and whether you are authenticating as a user or a service account.

Understanding the authentication flow is critical for securely developing applications that interact with the GEE API. It ensures that only authorized entities can access and process geospatial data, protecting both the user's data and Google's infrastructure. The choice between user account and service account authentication depends on the application's deployment environment and its need for user interaction.

Supported authentication methods

Google Earth Engine supports two primary authentication methods, both based on the OAuth 2.0 framework:

  1. User Account Authentication: This method is suitable for interactive applications, development environments, and scenarios where a user's Google identity is directly involved. It typically involves a web-based consent flow where the user grants permission to the application to access their GEE resources.
  2. Service Account Authentication: Designed for server-to-server interactions, automated scripts, and non-interactive applications. A service account is a special type of Google account used by applications and virtual machines, not by individual users. It's identified by an email address and uses a private key file for authentication.

The following table summarizes the primary authentication methods for Google Earth Engine:

Method When to Use Security Level
OAuth 2.0 (User Account) Interactive applications, GEE Code Editor, local Python scripts requiring user consent, academic/non-commercial use. High; relies on Google's robust identity verification and user consent.
OAuth 2.0 (Service Account) Automated workflows, server-side applications, cloud functions, commercial deployments requiring programmatic access without user interaction. High; requires careful management of private keys and IAM roles within a GCP project.

For commercial use, integration with a Google Cloud Project is mandatory, which enables the use of Identity and Access Management (IAM) to control permissions for both user and service accounts accessing GEE resources. This allows administrators to define granular roles and permissions, adhering to the principle of least privilege.

Getting your credentials

The process for obtaining credentials varies based on the authentication method you intend to use. Both methods typically begin within the Google Cloud Console.

For User Account Authentication (Python API)

  1. Enable the Earth Engine API: Navigate to the APIs & Services Dashboard in your Google Cloud Project and ensure the Earth Engine API is enabled.
  2. Install the Python Client Library: Install the earthengine-api package using pip: pip install earthengine-api.
  3. Initialize Earth Engine: In your Python script, run ee.Authenticate() and then ee.Initialize(). The ee.Authenticate() call will open a web browser for you to sign in with your Google account and grant the necessary permissions. This generates and stores refresh tokens locally.

For User Account Authentication (JavaScript API / Code Editor)

When working in the Google Earth Engine Code Editor, user authentication is typically handled automatically through your Google account login. No explicit credential setup is required within the Code Editor itself, as it operates within your authenticated browser session. Access to resources is governed by the permissions associated with your signed-in Google account.

For Service Account Authentication (Python or JavaScript)

  1. Create a Google Cloud Project: If you don't have one, create a new project in the Google Cloud Console.
  2. Enable Earth Engine API: Within your GCP project, go to 'APIs & Services' > 'Dashboard' and ensure the Earth Engine API is enabled.
  3. Create a Service Account:
    • Navigate to 'IAM & Admin' > 'Service Accounts'.
    • Click '+ CREATE SERVICE ACCOUNT'.
    • Provide a name and description.
    • Grant the service account appropriate roles, such as 'Earth Engine Resource User' or a custom role with specific GEE permissions, to adhere to the principle of least privilege.
    • Click 'Done'.
  4. Create a Private Key:
    • On the Service Accounts page, click on the newly created service account's email address.
    • Go to the 'Keys' tab.
    • Click 'ADD KEY' > 'Create new key'.
    • Select 'JSON' as the key type and click 'CREATE'. This will download a JSON file containing your service account's private key. Store this file securely.
  5. Configure Environment:
    • Python: Set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your downloaded JSON key file. Alternatively, you can pass the key file path directly to ee.Authenticate(oauth_scopes=['https://www.googleapis.com/auth/earthengine'], private_key_file='path/to/your/key.json').
    • JavaScript (Node.js): You can use the google-auth-library to authenticate with the service account key.

Authenticated request example

Here's a basic example demonstrating how to authenticate and make a simple request using the Python API with both user and service account credentials.

Python User Account Authentication

This method initiates a browser-based authentication flow.


import ee

# Authenticate and initialize Earth Engine
# This will open a browser window for you to log in with your Google account.
# It stores credentials locally for future use.
ee.Authenticate()
ee.Initialize()

# Example: Print the first image in the Landsat 8 collection
image = ee.Image('LANDSAT/LC08/C02/T1_SR/LC08_044034_20200318')
print(image.getInfo())

Python Service Account Authentication

This method uses a service account key file for non-interactive authentication.


import ee
import os

# Set the path to your service account key file
# Replace 'path/to/your/service_account_key.json' with the actual path
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'path/to/your/service_account_key.json'

# Initialize Earth Engine using the service account credentials
ee.Initialize()

# Example: Print the first image in the Sentinel-2 collection
image = ee.Image('COPERNICUS/S2_SR/20210101T100000_20210101T100000_T31TEJ')
print(image.getInfo())

Security best practices

Securing your Google Earth Engine access is crucial, especially when handling sensitive data or operating in a commercial context. Adhering to these best practices can mitigate risks:

  • Principle of Least Privilege: Grant only the necessary permissions to user and service accounts. For service accounts, create custom roles with specific GEE permissions rather than broad roles like 'Editor' or 'Owner'. Regularly review and update these permissions as requirements change. More information on managing access to Google Cloud resources can be found in the Google Cloud IAM documentation.
  • Secure Service Account Keys: Treat service account JSON key files as highly sensitive secrets. Never commit them to version control systems (e.g., Git). Store them in secure locations, such as Google Secret Manager, HashiCorp Vault, or environment variables in production environments. Rotate service account keys regularly.
  • OAuth Scopes: When using user account authentication, request only the minimum necessary OAuth 2.0 scopes. For Earth Engine, the primary scope is https://www.googleapis.com/auth/earthengine. Requesting fewer scopes reduces the potential impact of a compromised token.
  • Regular Audits: Periodically review your Google Cloud Project's IAM policies and activity logs to ensure that only authorized entities are accessing Earth Engine resources. Google Cloud provides extensive logging capabilities that can track API calls and authentication events.
  • Use Environment Variables: Avoid hardcoding credentials directly into your code. Instead, use environment variables (e.g., GOOGLE_APPLICATION_CREDENTIALS) to provide the path to service account keys or other sensitive information.
  • Multi-Factor Authentication (MFA): For user accounts accessing GEE, enforce multi-factor authentication (also known as 2-Step Verification) for all users within your Google Workspace or Cloud Identity organization. This adds an extra layer of security beyond just a password.
  • Stay Updated: Keep your earthengine-api Python client library and other dependencies updated to benefit from the latest security patches and features.