Home
Get a token for a user

Authorizing Agents with Arcade

In this guide, you'll learn how to use Arcade AI to obtain user authorization for accessing third-party services, without using Arcade for tool execution or definition.

This can be useful when you need to manage authorization flows in your application.

Prerequisites

pip install arcadepy google-api-python-client google-auth-httplib2 google-auth-oauthlib

Initialize the Arcade client

Create an instance of the Arcade client:

from arcadepy import Arcade
 
client = Arcade()  # Automatically finds the `ARCADE_API_KEY` env variable

Initiate an authorization request

Use client.auth.start to initiate the authorization process:

# Start the authorization process
auth_response = client.auth.start(
    user_id=user_id,
    provider="google",
    scopes=["https://www.googleapis.com/auth/gmail.readonly"],
)

Guide the user through authorization

If authorization is not completed, prompt the user to visit the authorization URL:

if auth_response.status != "completed":
    print("Please complete the authorization challenge in your browser:")
    print(auth_response.authorization_url)

Poll for authorization status

Wait for the user to complete authorization and poll for the status:

# Wait for the authorization to complete
auth_response = client.auth.wait_for_completion(auth_response)

Use the obtained token

Once authorization is complete, you can use the obtained token to access the third-party service:

from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
 
# Use the token from the authorization response
creds = Credentials(auth_response.context.token)
service = build("gmail", "v1", credentials=creds)
 
# Now you can use the Google API
results = service.users().labels().list(userId="me").execute()
labels = results.get("labels", [])
print("Labels:", labels)

How it works

By using client.auth.start and client.auth.wait_for_completion, you leverage Arcade AI to manage the OAuth flow for user authorization.

Arcade AI handles the authorization challenges and tokens, simplifying the process for you.

Next steps

Integrate this authorization flow into your application, and explore how you can manage different providers and scopes.