Skip to content

Commit

Permalink
Fixed the API authentication Python script
Browse files Browse the repository at this point in the history
  • Loading branch information
NeelamRG committed Feb 19, 2025
1 parent 2faa29f commit 993e61e
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions GettingStarted/APIAuthentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,52 @@ Example curl response
## Python

```
{
import json
import os
import requests
def _get_token(config=None):
"""Get token for interacting with the Earth Data Store API.
By default, Earth Data Store will look for environment variables called
EDS_AUTH_URL, EDS_SECRET and EDS_CLIENT_ID.
Parameters
----------
config : str | dict, optional
A JSON string or a dictionary with the credentials for the Earth Data Store.
presign_urls : bool, optional
Use presigned URLs, by default True
Returns
-------
token : str
"""
if config is None:
config = os.getenv
auth_url = config("EDS_AUTH_URL")
secret = config("EDS_SECRET")
client_id = config("EDS_CLIENT_ID")
if auth_url is None or secret is None or client_id is None:
raise AttributeError(
"You need to have env : EDS_AUTH_URL, EDS_SECRET and EDS_CLIENT_ID"
)
token_response = requests.post(
auth_url,
data={"grant_type": "client_credentials"},
allow_redirects=False,
auth=(client_id, secret),
)
token_response.raise_for_status()
return json.loads(token_response.text)["access_token"]
print(_get_token())
}
from dotenv import load_dotenv
# Loading secrets from environment variables
# By default, Earth Data Store will look for environment variables called
# EDS_AUTH_URL, EDS_SECRET and EDS_CLIENT_ID
# Ensure environment variables are set before running this script.
# You can set them in your terminal session or add them permanently to your shell configuration
# (e.g., .bash_profile, .bashrc) using the following format:
#
# export CLIENT_ID="your_client_id"
# export CLIENT_SECRET="your_client_secret"
# export AUTH_TOKEN_URL="your_auth_token_url"
#
# Alternatively, you can manage environment variables using a .env file and the python-dotenv package.
# Our account Information page will allow you to download your EDS.env file
load_dotenv("EDS.env")
CLIENT_ID = os.getenv("EDS_CLIENT_ID")
CLIENT_SECRET = os.getenv("EDS_SECRET")
AUTH_TOKEN_URL = os.getenv("EDS_AUTH_URL")
API_URL = os.getenv("EDS_API_URL")
# Setup requests session
session = requests.Session()
session.auth = (CLIENT_ID, CLIENT_SECRET)
def get_new_token(session):
"""Obtain a new authentication token using client credentials."""
token_req_payload = {"grant_type": "client_credentials"}
try:
token_response = session.post(AUTH_TOKEN_URL, data=token_req_payload)
token_response.raise_for_status()
tokens = token_response.json()
return tokens["access_token"]
except requests.exceptions.RequestException as e:
print(f"Failed to obtain token: {e}")
print(get_new_token(session))
```

## Postman
Expand All @@ -102,9 +103,9 @@ Below is the screenshot showing the **Authorization tab** in Postman and follow

1. Select the Type as <span style = "color: blue">OAuth 2.0</span>
2. Select the Grant Type as <span style = "color: blue">Client Credentials</span>
3. Enter the access token URL from the account information page above :point_up_2:
4. Enter the Client ID from the account information page above :point_up_2:
5. Enter the Client Secret from the account information page above :point_up_2:
3. Enter the access token URL from the account information page above
4. Enter the Client ID from the account information page above
5. Enter the Client Secret from the account information page above


![Postman](../Images/STACAPI/PostmanConfiguration.png)

0 comments on commit 993e61e

Please sign in to comment.