OAuthError
composio.auth.exceptions.OAuthError
Stack trace
Traceback (most recent call last):
File "app.py", line 45, in authenticate_user
token = composio.auth.oauth_exchange(code)
File "/usr/local/lib/python3.9/site-packages/composio/auth/oauth.py", line 78, in oauth_exchange
raise OAuthError('Invalid OAuth token or expired authorization code')
composio.auth.exceptions.OAuthError: Invalid OAuth token or expired authorization code Why it happens
This error happens when the OAuth authorization code is invalid, expired, or the token exchange request to Composio's OAuth server fails due to incorrect client credentials or network issues. It can also occur if the redirect URI does not match the registered URI in the Composio app settings.
Detection
Monitor OAuth token exchange responses and catch OAuthError exceptions; log the authorization code and client credentials used to detect mismatches or expired tokens before crashing.
Causes & fixes
Expired or invalid OAuth authorization code sent to Composio OAuth server
Ensure the authorization code is fresh and obtained from a valid user login flow; implement code expiration checks and refresh the code if needed.
Mismatch between redirect URI in the OAuth request and the one registered in Composio app settings
Verify that the redirect URI used in the OAuth flow exactly matches the URI registered in the Composio developer console, including trailing slashes and protocols.
Incorrect client ID or client secret configured in the app environment variables
Double-check and update the environment variables for COMPOSIO_CLIENT_ID and COMPOSIO_CLIENT_SECRET with the correct values from the Composio developer dashboard.
Network or connectivity issues preventing token exchange with Composio OAuth server
Implement retry logic with exponential backoff for token exchange requests and verify network connectivity to Composio's OAuth endpoints.
Code: broken vs fixed
import os
from composio.auth import oauth_exchange
code = 'expired_or_invalid_code'
# This line raises OAuthError due to invalid code
token = oauth_exchange(code)
print(f"Access token: {token}") import os
from composio.auth import oauth_exchange
os.environ['COMPOSIO_CLIENT_ID'] = 'your_client_id_here' # Set correct client ID
os.environ['COMPOSIO_CLIENT_SECRET'] = 'your_client_secret_here' # Set correct client secret
code = 'valid_authorization_code'
# Fixed: Use valid code and correct env vars for OAuth exchange
try:
token = oauth_exchange(code)
print(f"Access token: {token}")
except Exception as e:
print(f"OAuth authentication failed: {e}") Workaround
Wrap the oauth_exchange call in try/except OAuthError, then prompt the user to re-authenticate to obtain a fresh authorization code if the error occurs.
Prevention
Use automated token refresh flows and validate redirect URIs and client credentials during app deployment to prevent OAuth authentication failures in Composio apps.