The Client class is the primary entry point for the XDK. It handles authentication, session management, OAuth2 PKCE flows, and provides access to all API functionality through sub-clients.
from xdk import Client# App-only authentication with bearer tokenclient = Client(bearer_token="YOUR_BEARER_TOKEN")# User context with access tokenclient = Client(access_token="YOUR_ACCESS_TOKEN")
# After user authorizes and returns with codecode = request.args.get('code')token = client.exchange_code(code)print(f"Access token: {token['access_token']}")print(f"Refresh token: {token['refresh_token']}")# Client's access_token is now automatically setprint(f"Client access token: {client.access_token}")
Refreshes the OAuth2 access token using the refresh token. Automatically updates the client’s access_token property.Returns:Dict[str, Any] - New token dictionaryRaises:ValueError if OAuth2 credentials not configured
# Check if token is expired and refresh if neededif client.is_token_expired(): new_token = client.refresh_token() print(f"New access token: {new_token['access_token']}")
from xdk import Client# Initialize client with OAuth2client = Client( client_id="YOUR_CLIENT_ID", client_secret="YOUR_CLIENT_SECRET", redirect_uri="http://localhost:3000/callback", scope=["tweet.read", "tweet.write", "users.read", "follows.read"])# Step 1: Get authorization URLauth_url = client.get_authorization_url(state="secure_random_state")print(f"Visit: {auth_url}")# Step 2: After user authorizes, exchange codecode = input("Enter the code from callback: ")token = client.exchange_code(code)print(f"Authenticated! Access token: {token['access_token'][:20]}...")# Step 3: Use the client to make API callsme = client.users.get_me()print(f"Logged in as: {me['data']['username']}")# Create a postpost = client.posts.create_post(text="Hello from XDK!")print(f"Posted: {post['data']['id']}")# Step 4: Refresh token when expiredif client.is_token_expired(): client.refresh_token() print("Token refreshed!")