2

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)

2 Answers 2

0

Well, after digging in more with request in python I have parsed the entire return of my requsts.post()

I have not succeeded with that method.

So, i went to the EarthData login and added all the LPDAAC apps to have access and

GENERATED a BEARER TOKEN

just like I need...BearerToken

This may sum up my current question, however the program itself is not working, even with LPDAAC access granted the EarthData Bearer Token will not give me access to the server in the script, maybe it must be encoded somehow... anyway...this is the output and the script...

#!/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()

########################################################################################################################################
########################################################################################################################################
#login
username = 'user'
password = 'password!'
api = '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&output=json'
#bare token
token = 'token from earthdata' 
#earthdata login api access token

request = r.post('{}login'.format(api), auth=(username, password))


response = (request.request._cookies._cookies)
print ('request')
print (request)
print ('response')
print (response)
cookiejar1 = (response['urs.earthdata.nasa.gov'])
cookiejar2 = (response['.earthdata.nasa.gov'])
print ('cookiejar1')
print (cookiejar1)
print ('cookiejar2')
print (cookiejar2)
cookielid1 = (cookiejar1['/'])
cookielid2 = (cookiejar2['/'])
print ('cookielid1')
print ('cookielid2')
print (cookielid1)
print (cookielid2)
token2 = cookielid1['_urs-gui_session'].value
print ('cookietoken')
print (token2)


print (request.request.__dict__.keys())
print (request.request._cookies)


########################################################################################################################################
########################################################################################################################################
#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:')
print (str(task))
print()
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)

########################################################################################################################################
########################################################################################################################################
# retrieve the task
task_id = taskid 
response = requests.get('https://lpdaacsvc.cr.usgs.gov/appeears/api/task/{0}'.format(task_id),headers={'Authorization': 'Bearer {0}'.format(token)}) 
task_response = response.json() 
print(task_response)
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) 

{'message': "You don't have the permission to access the requested resource. It is either read-protected or not readable by the server."} ----even with the EarthData Token that has LPDAAC access...?

output:

enter image description here

0

I share one solution for the first part of the problem. This code obtain and generate a token to use in the API Earthdata from NASA.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module to check and generate tokens from API earth data
"""

#Importaciones
import urllib.error
import urllib.request
from base64 import b64encode
import json

# Inicializacion de variables
userAndPass = b64encode(b"USERNAME:PASSWORD").decode("ascii")
url_api_login = 'https://urs.earthdata.nasa.gov/api/users/tokens'
url_api_get_token = 'https://urs.earthdata.nasa.gov/api/users/token'


def check_token():      
    r = urllib.request.Request(url_api_login, None, {})
    r.add_header('Authorization' , 'Basic %s' %  userAndPass)
    response = urllib.request.urlopen(r)    
    respose_read = response.read()
    json_response = json.loads(respose_read)
    if (json_response == []):
        return None
    else:
        # return first token
        return json_response[0]
    
def obtain_token(): 
    r = urllib.request.Request(url_api_get_token, b'', {})
    r.add_header("Authorization" , "Basic %s" %  userAndPass)
    response = urllib.request.urlopen(r)
    respose_read = response.read()  
    json_response = json.loads(respose_read)    
    return json_response

def return_access_token():
    token = check_token()
    if token is None:
        token = obtain_token()  
    return token['access_token']
        
x = return_access_token()
print(x)

Calling the function return_access_token, it check if your user have any token generated otherwise it generate another one.

I hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.