The next NASA open API that I wanted to play with is “Imagery” (https://api.nasa.gov/api.html#imagery) which retrieves a Landsat 8 image for a supplied location. I created a program but so far it is not working as expected.
——
#!/bin/python3
#NASA Earth Imagery
#www.geektechstuff.com
#api.nasa.gov
#Modules to import
import json, requests
#Variables to use
#float – Latitude
lat=”
#float – Longitude
lon=”
#float – width and height of image in degress, default is 0.025
dim=”
#YYYY-MM-DD format.
date=”
#string – API Key – provided free by api.nasa.gov
api=’DEMO_KEY’
year=”
month=”
day=”
print(‘Hi and welcome to GeekTechStuff\’s NASA Earth Image searcher’)
print(”)
year=input(‘Please enter a year in YYYY format, e.g. 2018:’)
month=input(‘Please enter a month in MM format, e.g. 03 for March:’)
day=input(‘Please enter a numerical day in DD format, i.e. 01 to 31:’)
date=year+’-‘+month+’-‘+day
lat=input(‘Please input latitude co-ordinates:’)
lon=input(‘Please input longitude co-ordinates:’)
#Request JSON
headers={‘User-Agent’: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/604.5.6 (KHTML, like Gecko) Version/11.0.3 Safari/604.5.6’}
url=str(url)
print(url)
response=requests.get(url,headers=headers)
#response.raise_for_status()
nasa_reply=json.loads(response.text)
print(nasa_reply)
——
However, my code fails. Time to debug it;
At first I got the error message:
Traceback (most recent call last):
File “/Users/gary/Documents/nasa_earth_image.py”, line 44, in <module>
response.raise_for_status()
File “/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/models.py”, line 935, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: http://api.nasa.gov/planetary/earth/imagery/?lon=-2.24&lat=53&date=2016-03-03&cloud_score=True&api_key=DEMO_KEY
I tried removing the response.raise_for_status() and this got me a different error message:
{‘error’: {‘code’: ‘HTTPS_REQUIRED’, ‘message’: ‘Requests must be made over HTTPS. Try accessing the API at: https://api.nasa.gov/planetary/earth/imagery/?lon=-2.24&lat=53&date=2016-03-03&cloud_score=True&api_key=DEMO_KEY’}}
From the look of it my Python code is sending the URL request over HTTP instead of HTTPS. I added in some headers just in case the NASA API site was expecting a web browser to be making the response. Same issue,
I checked to see if my MacBook had done any recent updates – not in recent weeks.
I tried some of my other recent Python programs that utilise HTTPS – they work.
I reinstalled the SSL certificates that Python uses (via Applications/Python 3.6/Install Certificates.command) and then reran my program – same issue.
I’ll continue to try and find the issue and post an update when I have one….