Response 401 using requests.get in Python3.7 and 3.9

Use this Forum to find information on, or ask a question about, NASA Earth Science data.
Post Reply
yusriy
Posts: 3
Joined: Wed Aug 08, 2018 9:29 am America/New_York
Answers: 0

Response 401 using requests.get in Python3.7 and 3.9

by yusriy » 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.

Tags:

yusriy
Posts: 3
Joined: Wed Aug 08, 2018 9:29 am America/New_York
Answers: 0

Re: Response 401 using requests.get in Python3.7 and 3.9

by yusriy » Wed Jun 15, 2022 11:25 pm America/New_York

Solved! I was mistaken. The token was invalid even though it hadn't expired. The shorter token generated in the workshop is no longer in the right format. It was short, at about 64 characters, while the new token format has 568 characters. So, I generated the new token on the earthdata website and ran the generate NASA token code again, and it worked.

Post Reply