Wednesday, 7 November 2018

python - Planet API not being accepted for quick search but is on stats


As is similar on another post about this problem I receive the error,


"message": "Please enter your API key, or email and password.", 

"errors": []

when trying the script,


#API key stored as an enviroment variable
PLANET_API_KEY = os.getenv('api key here')

item_type = "REOrthoTile"

#API request object
search_request = {

"interval": "day",
"item_types": [item_type],
"filter": combined_filter
}

#first post request
search_result = \
requests.post(
'https://api.planet.com/data/v1/quick-search',
auth=HTTPBasicAuth(PLANET_API_KEY, ''),

json=search_request)

however I do not get the error when using the script,


#API key stored as an enviroment variable
PLANET_API_KEY = os.getenv('api key here')

item_type = "REOrthoTile"

#API request object
search_request = {

"interval": "day",
"item_types": [item_type],
"filter": combined_filter
}

#first post request
search_result = \
requests.post(
'https://api.planet.com/data/v1/stats',
auth=HTTPBasicAuth(PLANET_API_KEY, ''),

json=search_request)

another post (planet quick-search api not recognizing authentication?) had this same error and this (https://gis.stackexchange.com/a/239588/139012) response mentioned that a flag may be misconfigured, I cannot find a way to change this though.


To clarify, the API is accepted with the stats search result but not with the quick-search.


I removed my personal api key for security reasons but can send it for tests if needed



Answer



The line PLANET_API_KEY = os.getenv('api key here') is the problem here. There are two ways to get this to work.


First, if you are using an environment variable to store your API key so that it does not need to be hardcoded in each script or program, then you would need to make just a small change. For example, if you are storing the key in the variable API_KEY, then your code would look like this:


PLANET_API_KEY = os.getenv('API_KEY')


However, if you do want to use your API key directly in the code, the line would be:


PLANET_API_KEY = 'f4168dd427ed4a179c30189adfbc749e` # THIS IS NOT A REAL API KEY!

The first approach is better if you are going to have lots of scripts or will need to change API key at times. But you will need to set an environment variable, which is done differently depending on which operating system you are using.


No comments:

Post a Comment