Many APIs in Wildcard (Gmail, Google Calendar, Slack, etc.) use OAuth2 for authentication. This guide explains how to set up OAuth2 authentication for your applications.
If you’re looking for a hosted auth solution, contact us at info@wild-card.ai.
This snippet shows how you can get OAuth Base URL to start constructing the Authorization Code OAuth flow. See OAuth2 Authorization Code Flow for more information.
Copy
Ask AI
from wildcard_openai import WildcardClientfrom wildcard_core.models import Actionfrom wildcard_core.auth.oauth_helper import OAuth2Flowfrom typing import Setwildcard_client = WildcardClient( api_key="your_wildcard_api_key", index_name="your_index_name")# Returns a tuple of the auth flow and scopes for actiontool_name = Action.Gmail.MESSAGES_SENDoauth_flow: OAuth2Flow, scopes: Set[str] = ( await wildcard_client.get_oauth_info(tool_name))# The base OAuth URL to construct an OAuth flow# e.g. https://accounts.google.com/o/oauth2/authauthorization_url = oauth_flow.authorizationUrl
Now that we have the base OAuth URL, we can build the full URL to redirect the user to. We use requests_oauthlib to build the URL and generate a state parameter with the secrets library.
Copy
Ask AI
import secretsfrom requests_oauthlib import OAuth2Session# Generate a random state parameterstate = secrets.token_urlsafe(75)# URL the user will be redirected to after authorizationredirect_uri = "https://your-app.com/oauth/callback"# Boolean to indicate if PKCE is used. This is required for some services.is_pkce = True# Construct the full OAuth URLoauth = OAuth2Session( client_id=client_id, redirect_uri=redirect_uri, scope=' '.join(scopes), pkce='S256' if is_pkce else None)# Build the full OAuth URLauth_url, generated_state = oauth.authorization_url(authorization_url, state=state)code_verifier = oauth._code_verifier# Store this info securely in your database with the user info
Pass the generated auth url to your frontend application and redirect the user to it. Here’s an example of how you can do this in a frontend React application.
Set up an endpoint to handle the OAuth callback. This endpoint will be called by the service after the user has authorized the app. Store the returned credentials info in your database with the user info.
Copy
Ask AI
@app.route("/auth/callback", methods=["GET"])async def handle_oauth_callback(request: Request): # Handle the OAuth callback # Get the OAuth completion data from the request completion_data = await get_oauth_completion_data(request) # Store the completion data in your database await store_oauth_completion_data(completion_data) # Redirect the user to your app's home page return RedirectResponse(url="/")
5. Add OAuth Info to WildcardClient before running an Action
Before running an action, you need to add the OAuth info to the WildcardClient. This is done by calling the register_api_auth method. Here’s an example of how you can do this:
Copy
Ask AI
# Get the credentials from your databasecredentials, auth_type = await get_credentials_for_user(user_id, api_service)# Check if the credentials are of type API_KEY or OAUTH2if auth_type == AuthType.API_KEY: auth_config = ApiKeyAuthConfig( type= AuthType.API_KEY, key_value = credentials )else: auth_config = OAuth2AuthConfig( type= AuthType.OAUTH2, token = credentials["access_token"], token_type = credentials["token_type"], refresh_token = credentials["refresh_token"], expires_at=credentials["expires_at"], scopes = credentials["scopes"], )# Add the auth config to the WildcardClientapi_service = tool_name.get_api_service()wildcard_client.register_api_auth(api_service, auth_config)