Using The Long Lasting Token to Access and Download Data
It is recommended to save all of your files and folders within my-private-bucket, since everything that is outside of this folder is permanently deleted after 30 days from the last access into Coding.
How To Get The Long Lasting Token
All API keys and tokens are stored in a credentials.txt file located in the same directory as the script. Use the long-lasting token that can be generated here.
The generated token will expire in 90 days. Then, you just need to generate a new one by following the exact same procedure.
Creating The credentials.txt File
In Coding, you can create a new .txt file by clicking on "Text File" from the Launcher:
Next, you can populate your file with your keys and tokens in this format:
CLIENT_ID=offline-token
CLIENT_SECRET=p1eL7uonXs6MDxtGbgKdPVRAmnGxHpVE
OFFLINE_TOKEN=your_esamaap_longlasting_token_here
You only have to change OFFLINE_TOKEN, pasting your long lasting token generated before. The other two entries should be kept as they are in the above example.
Be careful to not use quotes around the values and ensure there are no trailing spaces or extra characters.
Downloading Data
You can refer to the following code example to download data by using your token:
import os
from pathlib import Path
import requests
# --- Path to credentials.txt ---
CREDENTIALS_FILE = Path('/your/path/credentials.txt').resolve().parent / "credentials.txt" # Insert the .txt path
def load_credentials(file_path=CREDENTIALS_FILE):
"""Read key-value pairs from a credentials file into a dictionary."""
creds = {}
if not file_path.exists():
raise FileNotFoundError(f"Credentials file not found: {file_path}")
with open(file_path, "r") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
key, value = line.split("=", 1)
creds[key.strip()] = value.strip()
return creds
# --- ESA MAAP API ---
def get_token():
"""Use OFFLINE_TOKEN to fetch a short-lived access token."""
creds = load_credentials()
OFFLINE_TOKEN = creds.get("OFFLINE_TOKEN")
CLIENT_ID = creds.get("CLIENT_ID")
CLIENT_SECRET = creds.get("CLIENT_SECRET")
print(CLIENT_SECRET)
if not all([OFFLINE_TOKEN, CLIENT_ID, CLIENT_SECRET]):
raise ValueError("Missing OFFLINE_TOKEN, CLIENT_ID, or CLIENT_SECRET in credentials file")
url = "https://iam.maap.eo.esa.int/realms/esa-maap/protocol/openid-connect/token"
data = {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"grant_type": "refresh_token",
"refresh_token": OFFLINE_TOKEN,
"scope": "offline_access openid"
}
response = requests.post(url, data=data)
response.raise_for_status()
response_json = response.json()
access_token = response_json.get('access_token')
if not access_token:
raise RuntimeError("Failed to retrieve access token from IAM response")
return access_token
def download_product(product_url: str, output_filename: str):
"""Download a file from a URL using a refresh access token."""
print("Requesting access token...")
token = get_token()
headers = {"Authorization": f"Bearer {token}"}
print(f"Downloading product:\n {product_url}")
with requests.get(product_url, headers=headers, stream=True) as r:
r.raise_for_status()
with open(output_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Download complete: {output_filename}")
if __name__ == "__main__":
# Example usage:
product_url = (
"https://catalog.maap.eo.esa.int/data/biomass-pdgs-01/"
"BiomassLevel1aIOC/2025/05/31/"
"BIO_S1_SCS__1M_20250531T222456_20250531T222517_C_G___M___C___T____F191_01_D9M4OI/"
"BIO_S1_SCS__1M_20250531T222456_20250531T222517_C_G___M___C___T____F191_01_D9M4OI/"
"annotation/bio_s1_scs__1m_20250531t222456_20250531t222517_c_g___m___c___t____f191_lut.nc"
)
output_file = "output_file_name.nc" # Substitute with the desired output file name
download_product(product_url, output_file)

No comments to display
No comments to display