# Make sure you have Python installed from https://www.python.org/
# Copy this code and save it to a file called authentication_example.py
# Install dependencies
# pip install requests
# User-Agent is set explicitly so default library agents (e.g. python-requests) are not rejected by the gateway.
import requests
# Fill ALL the below parameters with proper values
CLIENT_ID = "REPLACE-ME-WITH-CLIENT-ID" # This is the client ID
CLIENT_SECRET = "REPLACE-ME-WITH-CLIENT-SECRET" # This is the client secret
SCOPE = "REPLACE-ME-WITH-SCOPE" # This is the scope (looks like url)
SUBSCRIPTION_KEY = "REPLACE-ME-WITH-SUBSCRIPTION-KEY" # This is the subscription key
ORGANIZATION_NUMBER = "REPLACE-ME-WITH-ORGANIZATION-NUMBER" # This is the organization number
def check_response(response, step_name):
content_type = response.headers.get("Content-Type", "")
if not response.ok:
print(f"{step_name} failed with HTTP {response.status_code}")
print(f"Content-Type: {content_type}")
print(response.text[:500])
response.raise_for_status()
if "application/json" not in content_type:
print(f"{step_name} returned unexpected Content-Type: {content_type}")
print(response.text[:500])
raise RuntimeError(f"{step_name} did not return JSON")
return response.json()
# Request new Access token. No need for changing anything below
print("Requesting access token from identity provider")
token_response = requests.post(
"https://id.talenom.com/api/b2b/oauth2/v1.0/token",
data="grant_type=client_credentials&client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&scope=" + SCOPE,
headers={"Content-Type": "application/x-www-form-urlencoded", "User-Agent": "Easor-API-Client/1.0"},
timeout=30,
)
access_token = check_response(token_response, "Token request")["access_token"]
print("This is your access token:", access_token)
# Call organization-api and get basic organization information for the organization assigned in the parameters.
api_url = "https://apim.talenom.com/organization/v1/organization-info/" + ORGANIZATION_NUMBER
headers = {
"Authorization": "Bearer " + access_token,
"Content-Type": "application/json",
"Ocp-Apim-Subscription-Key": SUBSCRIPTION_KEY,
"User-Agent": "Easor-API-Client/1.0",
}
response = requests.get(api_url, headers=headers, timeout=30)
print(check_response(response, "API request"))
# Run with: python authentication_example.py