SDKsPython SDKOAuth 2.1
Client Credentials
Machine-to-machine authentication with the Python SDK.
Client Credentials
Obtain access tokens for service-to-service communication.
client_credentials()
from avnology_id import AvnologyId
client = AvnologyId(
base_url="https://api.id.avnology.com",
client_id="app_backend",
client_secret="sk_live_...",
)
tokens = client.oauth.client_credentials(scopes=["users:read", "organizations:read"])
print(tokens.access_token)
print(tokens.expires_in) # 3600 secondsToken caching
import time
from threading import Lock
class TokenCache:
def __init__(self, client: AvnologyId):
self._client = client
self._token = None
self._lock = Lock()
def get_token(self, scopes: list[str]) -> str:
with self._lock:
if self._token and time.time() < self._token.expires_at.timestamp():
return self._token.access_token
self._token = self._client.oauth.client_credentials(scopes=scopes)
return self._token.access_token
cache = TokenCache(client)
token = cache.get_token(["users:read"])Async token caching
import asyncio
class AsyncTokenCache:
def __init__(self, client: AsyncAvnologyId):
self._client = client
self._token = None
self._lock = asyncio.Lock()
async def get_token(self, scopes: list[str]) -> str:
async with self._lock:
if self._token and time.time() < self._token.expires_at.timestamp():
return self._token.access_token
self._token = await self._client.oauth.client_credentials(scopes=scopes)
return self._token.access_tokenSee also
- Tokens -- Token management
- Authorization -- Auth URL flows