Last active
May 21, 2025 19:56
-
-
Save rjhornsby/d5b9f9403e72d615f5392f6b393837ab to your computer and use it in GitHub Desktop.
python3 okta api example single threaded; no async required
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The python3 okta SDK is ridiculously complex requiring threading via async for even the most basic uses. | |
# The requests library is more than sufficient. | |
# | |
# The Okta API is somewhat sensitive to flooding and will throw a 429 error, so we'll add in some retry | |
# logic to help. | |
# | |
# Required environment variables (these correspond to the Terraform provider) | |
# OKTA_ORG_NAME - usually the first part of your Okta domain name ie 'mycorp' | |
# OKTA_BASE_URL - usually 'okta.com' | |
# OKTA_API_TOKEN - your API token, what Okta expects to come after 'SSWS' in the authorization header | |
# | |
import requests | |
from requests.adapters import HTTPAdapter | |
from urllib3.util.retry import Retry | |
OKTA_API_URL = f"https://{os.environ['OKTA_ORG_NAME']}.{os.environ['OKTA_BASE_URL']}/api/v1" | |
requests_retry_strategy = Retry( | |
backoff_factor=2, | |
total=3, | |
status_forcelist=[429, 500, 502, 503, 504] | |
) | |
rest_session = requests.Session() | |
rest_adapter = HTTPAdapter(max_retries=requests_retry_strategy) | |
rest_session.mount('https://', rest_adapter) | |
rest_session.headers.update({'Authorization': f"SSWS {os.environ["OKTA_API_TOKEN"]}"}) | |
# example - get a list of apps | |
response = rest_session.get(f"{OKTA_API_URL}/apps") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment