arcade-mcp/examples/get_auth_token.py
Eric Gustin 081865733a
Add examples (#136)
## PR Description
This PR adds 7 examples.

* `call_a_tool_directly_with_auth.py` - Simple example that uses Arcade
client to execute a tool that lists Gmail emails
* `call_a_tool_directly.py` - Simple example that uses Arcade client to
execute a tool that adds two numbers together
* `call_a_tool_with_llm.py` - Simple example that uses the LLM api to
star the arcade-ai repository
* `get_auth_token.py` - Simple example that gets a Google auth token and
then calls the Google API
* `call_multiple_tools_directly_with_auth.py` - A more involved example
that directly calls multiple spotify tools sequentially
* `call_multiple_tools_with_llm.py` - A more involved example that uses
an llm to call multiple spotify tools sequentially
* `simple_chatbot.py` - Simple chatbot that uses arcade tools and has
history

---------

Co-authored-by: Nate Barbettini <nathanaelb@gmail.com>
2024-11-06 11:02:41 -08:00

58 lines
2.1 KiB
Python

"""
This example demonstrates how to get an authorization token for a user and then use it to make a request to the Google API on behalf of the user.
"""
from arcadepy import Arcade
from google.oauth2.credentials import Credentials # pip install google-auth
from googleapiclient.discovery import build # pip install google-api-python-client
def get_auth_token(client: Arcade, user_id: str) -> str:
"""Get an authorization token for a user.
In this example, we are
1. Starting the authorization process for the Gmail Readonly scope
2. Waiting for the user to authorize the scope
3. Getting the authorization token
4. Using the authorization token to make a request to the Google API on behalf of the user
"""
# Start the authorization process
auth_response = client.auth.start(
user_id, "google", scopes=["https://www.googleapis.com/auth/gmail.readonly"]
)
if auth_response.status != "completed":
print(f"Click this link to authorize: {auth_response.authorization_url}")
auth_response = client.auth.wait_for_completion(auth_response)
return auth_response.context.token
def use_auth_token(token: str) -> None:
"""Use an authorization token to make a request to the Google API on behalf of a user.
In this example, we are
1. Using the authorization token that we got from the authorization process to make a request to the Google API
client.auth.wait_for_completion(auth_response)
"""
# Use the token from the authorization response
creds = Credentials(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)
if __name__ == "__main__":
cloud_host = "https://api.arcade-ai.com"
client = Arcade(
base_url=cloud_host, # Alternatively, use http://localhost:9099 if you are running Arcade locally, or any base_url if you're hosting elsewhere
)
user_id = "you@example.com"
token = get_auth_token(client, user_id)
use_auth_token(token)