Response 401 using requests.get in Python3.7 and 3.9
Posted: Tue Jun 14, 2022 12:41 pm America/New_York
Hello, forum! I hope somebody can assist me with this problem.
I'm getting response 401 when requesting data in Python on Juptyer.
I'm sure my token is correct, and my user name and password in a .netrc file are accurate. I was able to log on to Earthdata without issue.
The code I use is as below. FYI, the code was taught to me in an ECOSTRESS workshop a few weeks back. The organizer put me on the list that allows access to the ECOSTRESS product. It was accessible during the workshop too, but not now. I also cannot access OBDAAC's MODIS product as well.
#### CODE STARTS ####
CMR_OPS = 'https://cmr.earthdata.nasa.gov/search'
url = f'{CMR_OPS}/{"collections"}'
with open('../../.hidden_dir/edl_token.json') as js:
token = json.load(js)['access_token']
response = requests.get(url,
params={
'cloud_hosted': 'True',
'has_granules': 'True',
},
headers={
'Accept': 'application/json',
}
)
# I got "response 200."
for k, v in response.headers.items():
print(f'{k}: {v}')
# It prints the info ok.
response.headers['CMR-Hits']
# It shows the value '1227'
provider = 'LPCLOUD'
project = 'ECOSTRESS'
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/json',
}
response = requests.get(url,
params={
'cloud_hosted': 'True',
'has_granules': 'True',
'provider': provider,
'project': project,
},
headers=headers
)
response
# This gives me "Response [401]."
#### CODE ENDS ####
I am stuck here and do not know how to proceed. I tried accessing the same dataset on Earthdata, and I can download it there.
I'm getting response 401 when requesting data in Python on Juptyer.
I'm sure my token is correct, and my user name and password in a .netrc file are accurate. I was able to log on to Earthdata without issue.
The code I use is as below. FYI, the code was taught to me in an ECOSTRESS workshop a few weeks back. The organizer put me on the list that allows access to the ECOSTRESS product. It was accessible during the workshop too, but not now. I also cannot access OBDAAC's MODIS product as well.
#### CODE STARTS ####
CMR_OPS = 'https://cmr.earthdata.nasa.gov/search'
url = f'{CMR_OPS}/{"collections"}'
with open('../../.hidden_dir/edl_token.json') as js:
token = json.load(js)['access_token']
response = requests.get(url,
params={
'cloud_hosted': 'True',
'has_granules': 'True',
},
headers={
'Accept': 'application/json',
}
)
# I got "response 200."
for k, v in response.headers.items():
print(f'{k}: {v}')
# It prints the info ok.
response.headers['CMR-Hits']
# It shows the value '1227'
provider = 'LPCLOUD'
project = 'ECOSTRESS'
headers = {
'Authorization': f'Bearer {token}',
'Accept': 'application/json',
}
response = requests.get(url,
params={
'cloud_hosted': 'True',
'has_granules': 'True',
'provider': provider,
'project': project,
},
headers=headers
)
response
# This gives me "Response [401]."
#### CODE ENDS ####
I am stuck here and do not know how to proceed. I tried accessing the same dataset on Earthdata, and I can download it there.