I am trying to get started using the API using the EDL token I just generated on Nasa's Earthdata page. However, I tried running this code as a test and get the following error:
from pydap.client import open_url
import requests
import os
dataset_url = 'https://opendap.earthdata.nasa.gov/collections/C1276812863-GES_DISC/granules/M2T1NXSLV.5.12.4%3AMERRA2_100.tavg1_2d_slv_Nx.19800101.nc4?dap4.ce=/T2M[0:1:23][0:1:360][0:1:575]'
token_file_path = os.path.join(os.path.expanduser("~"), ".edl_token")
with open(token_file_path, 'r') as token_file:
token = token_file.read().strip()
my_session = requests.Session()
my_session.headers={"Authorization": token}
try:
dataset = open_url(dataset_url, session=my_session, protocol="dap4")
print(dataset['T2M'][:])
except OSError as e:
print('Error', e)
print('Please check that your .edl_token file has been properly generated, and that your .dodsrc files are in their correct locations.')
Error message:
HTTPError: 401 Unauthorized
Credentials (username, password) are invalid
This is happening even though the token is correct and stored in the correct folder on my Mac. Any help is greatly appreciated!
Issue with EDL token usage
-
- Posts: 2
- Joined: Tue Oct 29, 2024 7:24 am America/New_York
-
- Subject Matter Expert
- Posts: 22
- Joined: Wed Feb 16, 2022 4:38 pm America/New_York
- Has thanked: 1 time
Re: Issue with EDL token usage
Hello,
Thank you for reporting this error. The Pydap library will use the token method first, and if that fails, it will fall back to using the netrc/dodsrc if they exist. In this case, you actually uncovered an bug in our code, where the "Bearer: " string is missing from the request headers. Additionally, the error you experienced is most likely due to an incorrect username or password stored in a netrc file.
In the code, underneath the my_session = requests.Session() line, please use:
my_session.headers = {
'Authorization': f'Bearer {token}'
}
Additionally, you may need to re-generate the .edl_token file if it was not correctly stored. To check if it exists, you can use the terminal command "cat ~/.edl_token" to check if the token was stored.
We will correct the erroneous code in our tutorial as soon as we can, and please let us know if you experience any more issues!
Chris Battisto
GES DISC
Thank you for reporting this error. The Pydap library will use the token method first, and if that fails, it will fall back to using the netrc/dodsrc if they exist. In this case, you actually uncovered an bug in our code, where the "Bearer: " string is missing from the request headers. Additionally, the error you experienced is most likely due to an incorrect username or password stored in a netrc file.
In the code, underneath the my_session = requests.Session() line, please use:
my_session.headers = {
'Authorization': f'Bearer {token}'
}
Additionally, you may need to re-generate the .edl_token file if it was not correctly stored. To check if it exists, you can use the terminal command "cat ~/.edl_token" to check if the token was stored.
We will correct the erroneous code in our tutorial as soon as we can, and please let us know if you experience any more issues!
Chris Battisto
GES DISC
-
- Posts: 2
- Joined: Tue Oct 29, 2024 7:24 am America/New_York