I have been trying to download a set of files from NASA's website https://search.earthdata.nasa.gov/search. I was able to sign in and request the file but I get error.
Succesfully logged in
.
.
.
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Unauthorized
I tried requests, wget, urllib2
import requests
import os
import urllib2
destination = r'C:\PROJECTS\ShallowGW\MODIS\\'
os.chdir(destination)
session_req = requests.session()
home_url = 'https://urs.earthdata.nasa.gov/home'
response0 = session_req.get(home_url)
login_url = 'https://urs.earthdata.nasa.gov/login'
login_data = {'authenticity_token': '????',
'username': '*******',
'password': '*******'}
response1 = session_req.post(login_url, data=login_data,
headers=dict(referer=login_url))
try:
response1.raise_for_status()
print("Succesfully logged in")
except Exception as e:
print('Login failed')
response3 = session_req.get('https://search.earthdata.nasa.gov/search')
filepath = r'C:\PROJECTS\ShallowGW\MODIS\MODIS_LST_Links_2016.txt'
with open(filepath) as f_obj:
for line in f_obj:
urllib2.urlopen(line)
print(line)
I do not understand why it says I logged in then gives 401 HTTP ERROR. Text file has the links (e.g., https://e4ftl01.cr.usgs.gov//MODV6_Dal_E/MOLT/MOD11A1.006/2016.06.30/MOD11A1.A2016182.h12v04.006.2016241041516.hdf) to download the file. I can manually download them with the same line if I am signed in but it does not work with python. It is supposed to go through the loop and download each file. Please help!