GPT Actions 库 - Google 日历

2024 年 11 月 22 日
在 Github 中打开

简介

此页面为开发者构建特定应用程序的 GPT Action 提供说明和指南。在继续之前,请务必先熟悉以下信息

此 GPT Action 概述了如何连接到您的 Google 日历。它使用 OAuth 链接到您的 Google 帐户,使您能够在日历中创建、读取、更新和删除事件。

价值 + 示例业务用例

价值:用户现在可以利用 ChatGPT 的自然语言能力直接连接到他们的 Google 日历。

示例用例:

  • 您想要在日历中创建一个新事件。
  • 您想要根据特定条件搜索日历中的事件。
  • 您想要从日历中删除一个事件。

注意: 这是一个很好的 GPT 示例,可以使用 @ 从其他 GPT 调用它功能。您可以在我们的帮助网站上找到有关此功能的更多信息。

应用程序信息

应用程序先决条件

在开始之前,请确保您满足以下先决条件。

  • 一个具有 Google 日历访问权限的 Google 帐户。
  • 访问 Google 日历 API 并使用 Google Cloud Console 配置 OAuth 凭据的权限。

Google 日历配置步骤

启用 Google 日历 API

  • 访问 console.cloud.google.com
  • 在项目选择器中,选择您要用于此 GPT Action 的项目。如果您还没有项目,请单击 创建项目 按钮。
  • 创建新项目时,输入项目名称并选择您要关联的结算帐户。在本例中,选择了“无组织”。

您现在拥有一个 Google Cloud 项目,并准备好配置对 Google 日历的 API 访问权限。

  • 在快速访问菜单中,选择 API 和服务 >
  • 搜索 Google Calendar API(不是 DKIM)并单击它。
  • 单击 启用 按钮。

创建 OAuth 凭据

下一步是配置 OAuth 凭据,以允许您的 GPT Action 访问您的 Google 日历。

根据您当前的配置,您可能需要配置您的 OAuth 同意屏幕。我们将从那里开始。

  • 在左侧菜单中,单击 凭据
  • 现在单击 配置同意屏幕
  • 如果出现选项,请选择 转到新体验 并单击 开始使用
  • 输入您的应用名称,并在“用户支持电子邮件”下拉列表中选择您的电子邮件。
  • 选择“内部”受众,并输入联系人电子邮件。
  • 同意条款并单击 创建

我们现在准备好创建 OAuth 凭据。

  • 单击 创建 OAuth 凭据
  • 选择 Web 应用程序
  • 输入您的应用程序名称
  • 在“已授权的 JavaScript 来源”下,输入 https://chat.openai.comhttps://chatgpt.com
  • 目前,我们将 已授权的重定向 URI 留空。(我们稍后会回到这一点)
  • 单击 创建
  • 打开凭据页面,您将在屏幕右侧看到您的 OAuth 客户端 ID 和客户端密钥。

配置 OAuth 范围

接下来,配置 OAuth 客户端 ID 将有权访问的范围(或服务)。在本例中,我们将配置对 Google Calendar API 的访问权限。

  • 在左侧菜单中,单击 数据访问
  • 单击 添加或删除范围
  • 在右侧面板中,过滤 https://www.googleapis.com/auth/calendar
  • 在过滤后的结果中,选择第一个结果,范围应以 /auth/calendar 结尾
  • 单击 更新,然后单击 保存

GPT Action 配置步骤

我们现在准备好配置 GPT Action。首先,我们将配置 OAuth 设置,以允许 GPT 使用 Google 日历进行身份验证。

  • 在您的 GPT 中,创建一个 action。
  • 单击设置齿轮图标,然后选择 OAuth
  • 输入来自 Google Cloud Console 的 客户端 ID客户端密钥
  • 输入以下详细信息
    • 授权 URL: https://127.0.0.1/o/oauth2/auth
    • 令牌 URL: https://oauth2.googleapis.com/token
    • 范围: https://www.googleapis.com/auth/calendar
  • 将“令牌交换方法”保留为默认值。
  • 单击 保存
Google Calendar OAuth

我们现在可以输入 action 的 OpenAPI 架构。以下配置允许读取和创建事件。在“OpenAPI 架构”字段中输入此内容。

openapi: 3.1.0
info:
  title: Google Calendar API
  description: This API allows you to read and create events in a user's Google Calendar.
  version: 1.0.0
servers:
  - url: https://www.googleapis.com/calendar/v3
    description: Google Calendar API server
 
paths:
  /calendars/primary/events:
    get:
      summary: List events from the primary calendar
      description: Retrieve a list of events from the user's primary Google Calendar.
      operationId: listEvents
      tags:
        - Calendar
      parameters:
        - name: timeMin
          in: query
          description: The lower bound (inclusive) of the events to retrieve, in RFC3339 format.
          required: false
          schema:
            type: string
            format: date-time
            example: "2024-11-01T00:00:00Z"
        - name: timeMax
          in: query
          description: The upper bound (exclusive) of the events to retrieve, in RFC3339 format.
          required: false
          schema:
            type: string
            format: date-time
            example: "2024-12-01T00:00:00Z"
        - name: maxResults
          in: query
          description: The maximum number of events to return.
          required: false
          schema:
            type: integer
            default: 10
        - name: singleEvents
          in: query
          description: Whether to expand recurring events into instances. Defaults to `false`.
          required: false
          schema:
            type: boolean
            default: true
        - name: orderBy
          in: query
          description: The order of events. Can be "startTime" or "updated".
          required: false
          schema:
            type: string
            enum:
              - startTime
              - updated
            default: startTime
      responses:
        '200':
          description: A list of events
          content:
            application/json:
              schema:
                type: object
                properties:
                  items:
                    type: array
                    items:
                      type: object
                      properties:
                        id:
                          type: string
                          description: The event ID
                        summary:
                          type: string
                          description: The event summary (title)
                        start:
                          type: object
                          properties:
                            dateTime:
                              type: string
                              format: date-time
                              description: The start time of the event
                            date:
                              type: string
                              format: date
                              description: The start date of the all-day event
                        end:
                          type: object
                          properties:
                            dateTime:
                              type: string
                              format: date-time
                              description: The end time of the event
                            date:
                              type: string
                              format: date
                              description: The end date of the all-day event
                        location:
                          type: string
                          description: The location of the event
                        description:
                          type: string
                          description: A description of the event
        '401':
          description: Unauthorized access due to missing or invalid OAuth token
        '400':
          description: Bad request, invalid parameters
 
    post:
      summary: Create a new event on the primary calendar
      description: Creates a new event on the user's primary Google Calendar.
      operationId: createEvent
      tags:
        - Calendar
      requestBody:
        description: The event data to create.
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                summary:
                  type: string
                  description: The title of the event
                  example: "Team Meeting"
                location:
                  type: string
                  description: The location of the event
                  example: "Conference Room 1"
                description:
                  type: string
                  description: A detailed description of the event
                  example: "Discuss quarterly results"
                start:
                  type: object
                  properties:
                    dateTime:
                      type: string
                      format: date-time
                      description: Start time of the event
                      example: "2024-11-30T09:00:00Z"
                    timeZone:
                      type: string
                      description: Time zone of the event start
                      example: "UTC"
                end:
                  type: object
                  properties:
                    dateTime:
                      type: string
                      format: date-time
                      description: End time of the event
                      example: "2024-11-30T10:00:00Z"
                    timeZone:
                      type: string
                      description: Time zone of the event end
                      example: "UTC"
                attendees:
                  type: array
                  items:
                    type: object
                    properties:
                      email:
                        type: string
                        description: The email address of an attendee
                        example: "attendee@example.com"
              required:
                - summary
                - start
                - end
      responses:
        '201':
          description: Event created successfully
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                    description: The ID of the created event
                  summary:
                    type: string
                    description: The event summary (title)
                  start:
                    type: object
                    properties:
                      dateTime:
                        type: string
                        format: date-time
                        description: The start time of the event
                  end:
                    type: object
                    properties:
                      dateTime:
                        type: string
                        format: date-time
                        description: The end time of the event
        '400':
          description: Bad request, invalid event data
        '401':
          description: Unauthorized access due to missing or invalid OAuth token
        '500':
          description: Internal server error

如果成功,您将在配置屏幕底部看到两个端点出现。

Google Calendar Action Endpoints

设置回调 URL

现在我们已经配置了 OAuth 设置并设置了 OpenAPI 架构,ChatGPT 将生成一个回调 URL。您需要将此 URL 添加到 Google Cloud Console 中的 已授权的重定向 URI

退出 ChatGPT 中的 action 配置屏幕并滚动到底部。在那里,您将找到生成的回调 URL。

注意: 如果您修改 OAuth 设置,将生成新的回调 URL,这也需要添加到 Google Cloud Console 中的 已授权的重定向 URI。”

Google Calendar Callback URL

复制此 URL 并将其添加到 Google Cloud Console 中的 已授权的重定向 URI,然后单击 保存

Google Calendar Callback URL

测试 Action

配置好 action 后,您现在可以在 ChatGPT 中对其进行测试。首先向您的 GPT 提出一个测试问题,例如:我今天有什么活动? 如果这是您第一次使用该 action,系统将提示您授权该 action。单击 使用 googleapis.com 登录 并按照提示授权该 action。

Google Calendar Sign In

授权后,您应该会看到日历中的结果。

Google Calendar results