I have attempted logging in to LP DAAC data download, AppEEARS API, with Python, using requests per the AppEEARS API tutorial. There are two tutorials with slightly different methods at obtaining a TOKEN via an Earthdata login.
Tutorials:
I am logging in to download GEDI data via an AREA REQEST.
Primarily, the issue is with login, via Earthdata.
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import time
import requests as r
import getpass, pprint, time, os, cgi, json
import geopandas as gpd
import requests
import ee; ee.Initialize()
########################################################################################################################################
########################################################################################################################################
#Earthdata login ...not working yet
username = 'user'
password = 'pass'
#earthdata login api access token
#request = requests.post('https://lpdaacsvc.cr.usgs.gov/appeears/login', auth=(username, password)) #recieved a 404
#request = requests.post('http://urs.earthdata.nasa.gov', auth=(username, password)) #recieved a 200 all good
request = requests.post('https://urs.earthdata.nasa.gov/oauth/authorize?client_id=ZAQpxSrQNpk342OR77kisA&response_type=code&redirect_uri=https%3A%2F%2Flpdaacsvc.cr.usgs.gov%2Fappeears%2Flogin&state=%2F', auth=(username, password)) #recieved a 200 all good
#token response, error
#response = request.json
#token = response['token']
# debugging request and response
response = request
print ('response:')
print (response)
time.sleep(4)
print ('response.text:')
print (response.text)
time.sleep(2)
print ('response.json:')
jsonresponse = response.json
print (jsonresponse)
time.sleep(4)
print (str(jsonresponse))
time.sleep(4)
#responsejson=json.loads(str(jsonresponse))
#print (str(responsejson))
#print (jsonresponse['token'])
#time.sleep(7)
print('response.status_code:')
print(response.status_code)
time.sleep(1)
########################################################################################################################################
########################################################################################################################################
#the rest of the program
#coordinates for GEDI tiff extraction
lat = 34
lng = -118
#zoom (zoom 1 - 22)
x = 12
#time-range
time1 = '02-15-2020'
tend = '03-15-2021'
########################################################################################################################################
########################################################################################################################################
This is the beginning of my Python program as described in tutorial 2 and what it has developed into with debugging getting response['token'] to equal something
The entire script is for visualization of GEDI AT lng, lat with zoom via a polygon, in a time series ... ultimately in Google Earth Engine as an image collection. I am very unsure of what layers to obstract in the GEDI JSON data that makes up the task. Anyway, much work to go, however I must Login first.
Here is the rest for a read...
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# enable debugging
import cgitb
cgitb.enable()
import time
import requests as r
import getpass, pprint, time, os, cgi, json
import geopandas as gpd
import requests
import ee; ee.Initialize()
########################################################################################################################################
########################################################################################################################################
#Earthdata login ...not working yet
username = 'user'
password = 'pass'
#earthdata login api access token
#request = requests.post('https://lpdaacsvc.cr.usgs.gov/appeears/login', auth=(username, password)) #recieved a 404
#request = requests.post('http://urs.earthdata.nasa.gov', auth=(username, password)) #recieved a 200 all good
request = requests.post('https://urs.earthdata.nasa.gov/oauth/authorize?client_id=ZAQpxSrQNpk342OR77kisA&response_type=code&redirect_uri=https%3A%2F%2Flpdaacsvc.cr.usgs.gov%2Fappeears%2Flogin&state=%2F', auth=(username, password)) #recieved a 200 all good
#token response, error
#response = request.json
#token = response['token']
# debugging request and response
response = request
print ('response:')
print (response)
time.sleep(4)
print ('response.text:')
print (response.text)
time.sleep(2)
print ('response.json:')
jsonresponse = response.json
print (jsonresponse)
time.sleep(4)
print (str(jsonresponse))
time.sleep(4)
#responsejson=json.loads(str(jsonresponse))
#print (str(responsejson))
#print (jsonresponse['token'])
#time.sleep(7)
print('response.status_code:')
print(response.status_code)
time.sleep(1)
########################################################################################################################################
########################################################################################################################################
#the rest of the program
#coordinates for GEDI tiff extraction
lat = 34
lng = -118
#zoom (zoom 1 - 22)
x = 12
#time-range
time1 = '02-15-2020'
tend = '03-15-2021'
########################################################################################################################################
########################################################################################################################################
# create the task request
task = {
"task_type": "area",
"task_name": "GEDI-LFTR",
"params":
{
"dates": [
{
"startDate": time1,
"endDate": tend
}],
"layers": [
{
"layer": "all_samples_sum",
"product": "GEDI01_B.001"
}],
"output":
{
"format":
{
"type": "geotiff"
},
"projection": "native"
},
"geo":
{
"type": "FeatureCollection",
"fileName": "User-Drawn-Polygon",
"features": [
{
"type": "Feature",
"properties":
{},
"geometry":
{
"type": "Polygon",
"coordinates": [
[
[lng+(.6458/x),lat+(.3540/x)],
[lng+((-1+(.8135))/x),lat+(.3540/x)],
[lng+((-1+(.8135))/x),lat+((-1+.8042)/x)],
[lng+(.6458/x),lat+((-1+.8042)/x)]
]
]
}
}]
}
}
}
print ('task created')
time.sleep(2)
########################################################################################################################################
########################################################################################################################################
# submit the task request
response = requests.post('https://lpdaacsvc.cr.usgs.gov/appeears/api/task', json=task, headers={'Authorization': 'Bearer {0}'.format(token)})
task_response = response.json()
task_response
print(task_response)
taskid= str(task_response)
taskid = (taskid[13:37])
print ('taskid:')
print (taskid)
time.sleep(2)
########################################################################################################################################
########################################################################################################################################
# bundle the task
task_id = taskid
response = requests.get('https://lpdaacsvc.cr.usgs.gov/appeears/api/bundle/{0}'.format(task_id))
bundle_response = response.json()
print(bundle_response)
time.sleep(2)
########################################################################################################################################
########################################################################################################################################
# download the task
#fileid = bundle_response['file_id']
#print ('fileid:')
#print (fileid)
#time.sleep(2)
# get a stream to the bundle file
#task_id = taskid
#file_id = fileid
#response = requests.get('https://lpdaacsvc.cr.usgs.gov/appeears/api/bundle/{0}/{1}'.format(task_id, file_id), stream=True)
#content_disposition = cgi.parse_header(response.headers['Content-Disposition'])[1]
#filename = os.path.basename(content_disposition['filename'])
#dest_dir = "/var/www/cgi-bin/GEDIdata"
#filepath = os.path.join(dest_dir, filename)
#os.makedirs(os.path.dirname(filepath), exist_ok=True)
#with open(filepath, 'wb') as f:
# for data in response.iter_content(chunk_size=8192):
# f.write(data)
########################################################################################################################################
########################################################################################################################################
#make data an ee image collection
GEDI = []
GEDI = os.listdir('/var/www/cgi-bin/GEDIdata/filename')
print ('GEDI:')
print (GEDI)
time.sleep(1)
GEDIcoll=ee.ImageCollection.fromImages(GEDI)
print (GEDIcoll)
GEDIbands = GEDIcoll.toBands()
print (GEDIbands)