Akamai EdgeGrid Authentication for Python
edgegrid-python provides the client authentication protocol for accessing Akamai's APIs, designed to integrate seamlessly with the popular `requests` library. It handles the cryptographic signing of requests required by the Akamai EdgeGrid specification. The current version is 2.0.5, with frequent releases primarily focused on security updates and dependency management, and less frequent but significant feature or breaking changes.
Warnings
- breaking Version 2.0.0 discontinued support for Python 2.7. The minimum supported Python version became 3.9.
- breaking Version 2.0.3 dropped support for Python 3.9. The minimum supported Python version is now 3.10.
- breaking As of v2.0.0, the `__init__` function of `EdgeGridAuth` and `EdgeGridAuthHeaders` now accepts `headers_to_sign` and `max_body` parameters exclusively as keyword-only arguments. Direct positional arguments for these will raise errors.
- gotcha Incorrect configuration of `.edgerc` file path or missing/incorrect environment variables are common causes of authentication failures. The library searches for `~/.edgerc` by default or can be pointed to a custom path.
- gotcha The `max_body` parameter (default 2048 bytes) truncates request bodies for signature generation if the body exceeds this size. For large POST/PUT requests, especially those with binary data, this can lead to signature mismatches if the API expects the full body to be signed.
Install
-
pip install edgegrid-python
Imports
- EdgeGridAuth
from akamai.edgegrid import EdgeGridAuth
- EdgeGridAuthHeaders
from akamai.edgegrid import EdgeGridAuthHeaders
Quickstart
import requests
import os
from akamai.edgegrid import EdgeGridAuth
# For simplicity, using environment variables.
# In production, use a .edgerc file (e.g., ~/.edgerc) and specify a section.
# Example .edgerc section:
# [default]
# host = your_api_host.akadns.net
# client_token = YOUR_CLIENT_TOKEN
# client_secret = YOUR_CLIENT_SECRET
# access_token = YOUR_ACCESS_TOKEN
# Configure from environment variables (less secure for production)
client_token = os.environ.get('AKAMAI_CLIENT_TOKEN', 'YOUR_CLIENT_TOKEN_HERE')
client_secret = os.environ.get('AKAMAI_CLIENT_SECRET', 'YOUR_CLIENT_SECRET_HERE')
access_token = os.environ.get('AKAMAI_ACCESS_TOKEN', 'YOUR_ACCESS_TOKEN_HERE')
host = os.environ.get('AKAMAI_HOST', 'https://your-api-host.akadns.net')
if client_token == 'YOUR_CLIENT_TOKEN_HERE' or \
client_secret == 'YOUR_CLIENT_SECRET_HERE' or \
access_token == 'YOUR_ACCESS_TOKEN_HERE' or \
host == 'https://your-api-host.akadns.net':
print("Warning: Please set AKAMAI_CLIENT_TOKEN, AKAMAI_CLIENT_SECRET, AKAMAI_ACCESS_TOKEN, and AKAMAI_HOST environment variables or update the example with actual credentials.")
print("Or configure a .edgerc file and initialize EdgeGridAuth with 'client_token=None' and 'section='your_section_name'")
else:
# Initialize EdgeGridAuth with credentials (from env vars in this case)
# For .edgerc file, use: auth = EdgeGridAuth(client_token=None, section='default')
auth = EdgeGridAuth(
client_token=client_token,
client_secret=client_secret,
access_token=access_token,
base_url=host
)
# Create a requests session and apply EdgeGridAuth
session = requests.Session()
session.auth = auth
# Example API call (replace with an actual Akamai API endpoint)
try:
response = session.get(f'{host}/diagnostic-tools/v2/locations') # Example path
response.raise_for_status() # Raise an exception for HTTP errors
print(f"Successfully authenticated and fetched data: {response.status_code}")
# print(response.json()) # Uncomment to see the response body
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e} - {e.response.text}")
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")