Skip to content

Instantly share code, notes, and snippets.

@willrayeo
Last active December 7, 2023 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willrayeo/8aa424384f272d3003a2dea6460cb07b to your computer and use it in GitHub Desktop.
Save willrayeo/8aa424384f272d3003a2dea6460cb07b to your computer and use it in GitHub Desktop.
# Import credentials
# from creds import *
def get_access_token(username: str, password: str) -> str:
data = {
"client_id": "cdse-public",
"username": username,
"password": password,
"grant_type": "password",
}
try:
r = requests.post(
"https://identity.dataspace.copernicus.eu/auth/realms/CDSE/protocol/openid-connect/token",
data=data,
)
r.raise_for_status()
except Exception as e:
raise Exception(
f"Access token creation failed. Reponse from the server was: {r.json()}"
)
return r.json()["access_token"]
access_token = get_access_token("USERNAME", "PASSWORD")
access_token = get_access_token(
getpass.getpass("Enter your username"),
getpass.getpass("Enter your password"),
)
@anikfal
Copy link

anikfal commented Nov 7, 2023

Does it mean that the credentials should be stored in a creds.py file?

@willrayeo
Copy link
Author

The most secure way to parse your credentials would be to use the getpass library. However, you can also store them in a .py file named as you wish.

@ibaris
Copy link

ibaris commented Dec 7, 2023

This code is incomplete! The imports are missing, documentation is missing and the purpose is not clear.

@anikfal
Copy link

anikfal commented Dec 7, 2023

@ibaris
This is a snippet, not a complete code. You can use the repository below (directory sentinel_download_new_api) for downloading SENTINEL2 in the SAFE format, for a group of lat/lon points:
https://github.com/anikfal/GIS_solutions/tree/master

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment