启用 Agent 使用 Google Calendar 来查看和安排会议。
先决条件
安装依赖项
设置 Google 项目和 OAuth
参考:https://developers.google.com/calendar/api/quickstart/python
-
启用 Google Calendar API
-
前往 API 和服务 -> OAuth 同意屏幕
-
选择用户类型
- 如果您是 Google Workspace 用户,请选择内部。
- 否则,请选择外部。
-
填写应用详情(应用名称、徽标、支持电子邮件等)。
-
选择范围
- 点击添加或删除范围。
- 搜索 Google Calendar API(确保您已启用 Google Calendar API,否则范围将不可见)。
- 根据需要选择范围
- 从下拉列表中勾选
/auth/calendar
范围
- 保存并继续。
-
添加测试用户
- 点击添加用户并输入您希望在测试期间允许的用户电子邮件地址。
- 注意:在“测试”模式下,只有这些用户才能访问应用的 OAuth 功能。
其他任何用户将收到访问被拒绝的错误。
- 要使应用可供所有用户使用,您需要将应用的
状态移至“生产”。
在此之前,请确保在应用使用敏感或受限范围的情况下,已获得 Google 的完全验证。
- 点击返回到控制台。
-
生成 OAuth 2.0 客户端 ID
- 前往凭据。
- 点击创建凭据 -> OAuth 客户端 ID
- 选择应用类型为桌面应用。
- 下载 JSON。
-
使用 Google Calendar 工具
- 将下载的凭据路径作为
credentials_path
传递给 Google Calendar 工具。
- 可选:设置
token_path
参数以指定工具应创建 token.json
文件的位置。
token.json
文件用于存储用户的访问和刷新令牌,并在授权流程中自动创建(如果尚不存在)。
- 如果未显式提供
token_path
,文件将在默认位置(您当前的工作目录)创建。
- 如果选择指定
token_path
,请确保您提供的目录具有写入权限,因为应用程序在身份验证过程中需要创建或更新此文件。
以下 Agent 将使用 GoogleCalendarTools 来查找今天的事件。
cookbook/tools/googlecalendar_tools.py
from agno.agent import Agent
from agno.tools.googlecalendar import GoogleCalendarTools
import datetime
import os
from tzlocal import get_localzone_name
agent = Agent(
tools=[GoogleCalendarTools(credentials_path="<PATH_TO_YOUR_CREDENTIALS_FILE>")],
show_tool_calls=True,
instructions=[
f"""
You are scheduling assistant . Today is {datetime.datetime.now()} and the users timezone is {get_localzone_name()}.
You should help users to perform these actions in their Google calendar:
- get their scheduled events from a certain date and time
- create events based on provided details
"""
],
add_datetime_to_instructions=True,
)
agent.print_response("Give me the list of todays events", markdown=True)
参数 | 类型 | 默认 | 描述 |
---|
credentials_path | str | None | 包含 OAuth 2.0 Client ID 的 credentials.json 文件的路径。 |
token_path | str | None | 存储用户访问和刷新令牌的 token.json 文件的路径。 |
函数 | 描述 |
---|
list_events | 列出用户主要日历中的事件。 |
create_event | 在用户主要日历中创建新事件。 |
开发者资源
Responses are generated using AI and may contain mistakes.