from grantex import (
Grantex,
AuthorizeParams,
ExchangeTokenParams,
generate_pkce,
)
# 1. Generate PKCE pair
pkce = generate_pkce()
with Grantex(api_key="gx_live_...") as client:
# 2. Start authorization with PKCE
auth = client.authorize(AuthorizeParams(
agent_id="agt_abc123",
user_id="user_xyz",
scopes=["calendar:read"],
redirect_uri="https://myapp.com/callback",
code_challenge=pkce.code_challenge,
code_challenge_method=pkce.code_challenge_method,
))
print(f"Send user to: {auth.consent_url}")
# 3. User approves, you receive the code at your callback
code = "code_from_redirect"
# 4. Exchange with PKCE verifier
result = client.tokens.exchange(ExchangeTokenParams(
code=code,
agent_id="agt_abc123",
code_verifier=pkce.code_verifier,
))
print(f"Grant token: {result.grant_token}")
print(f"Grant ID: {result.grant_id}")
print(f"Scopes: {result.scopes}")