快速入门介绍了如何设置和运行调用 Google Workspace API。
Google Workspace 快速入门使用 API 客户端库来处理一些 身份验证和授权流程的详细信息。我们建议 您需要为自己的应用使用客户端库本快速入门使用 适用于测试的简化身份验证方法 环境对于生产环境,我们建议您了解 身份验证和授权 早于 选择访问凭据 适合您应用的广告格式
创建 Google Apps 脚本 向 Google Chat API 发送请求
目标
- 配置环境。
- 创建并配置脚本。
- 运行脚本。
前提条件
- 有权访问 Google Chat 的 Google Workspace 商务版或企业版账号。
配置 Cloud 项目
如果您使用新的 Google Cloud 项目来完成本快速入门,则需要执行以下操作: 进行配置并将自己添加为测试用户。如果您已经 请跳到下一部分。
在 Google Cloud 控制台中打开您的 Cloud 项目
如果尚未打开,请打开您要使用的 Cloud 项目 示例:
- 在 Google Cloud 控制台中,前往选择项目页面。
- 选择您要使用的 Google Cloud 项目。或者,点击创建项目,然后按照屏幕上的说明操作。如果您创建的是 Google Cloud 项目,则可能需要为项目启用结算功能。
启用 Chat API
在使用 Google API 之前,您需要先在 Google Cloud 项目中启用这些 API。 您可以在单个 Google Cloud 项目中启用一个或多个 API。在 Google Cloud 控制台中,启用 Google Chat API。
配置 OAuth 权限请求页面
- 在 Google Cloud 控制台中,点击“菜单”图标 > API 和服务 > OAuth 同意屏幕。
- 对于用户类型,选择内部,然后点击创建。
- 填写应用注册表单,然后点击保存并继续。
现在,您可以跳过添加范围的步骤,然后点击保存并继续。 以后,如果您要创建一款应用供用户使用 Google Workspace 组织,您必须将用户类型更改为外部,然后执行以下操作: 添加您的应用所需的授权范围。
- 查看应用注册摘要。如需进行更改,请点击修改。如果应用 点击 Back to Dashboard(返回信息中心)。
配置 Google Chat 应用
要调用 Google Chat API,您必须配置 Google Chat 应用。对于任何写入请求,Google Chat 使用 以下信息。
在 Google Cloud 控制台中,前往 Chat API 配置页面:
在 Application info(应用信息)下,输入以下信息:
- 在应用名称字段中,输入
Chat API quickstart app
。 - 在头像网址字段中,输入
https://developers.google.com/chat/images/quickstart-app-avatar.png
。 - 在说明字段中,输入
Quickstart for calling the Chat API
。
- 在应用名称字段中,输入
在互动功能下,点击启用互动功能 切换到关闭位置,以便停用 Chat 应用。
点击保存。
创建脚本
- 前往 script.google.com/create 创建新脚本。
- 将脚本编辑器的内容替换为以下代码:
- 点击“保存”。
- 点击未命名项目,输入 快速入门,然后点击重命名。
配置脚本
复制 Cloud 项目编号
- 在 Google Cloud 控制台中,点击“菜单”图标 > IAM 和管理 > 设置。
- 复制项目编号字段中的值。
关联您的 Google Cloud 项目
- 打开 Apps 脚本项目。
- 点击 Project Settings 图标 。
- 在 Google Cloud Platform (GCP) 项目下,点击更改项目。
- 在 GCP 项目编号中,粘贴 Google Cloud 项目编号。
- 点击设置项目。
启用 Google Chat API
- 打开 Apps 脚本项目。
- 点击 Project Settings 图标 。
- 在常规设置下,启用显示“appsscript.json”清单文件 编辑器中的。
- 点击 Editor
appscript.json
,并将内容替换为以下代码:
,然后选择
文件
运行示例
在 Apps 脚本编辑器中,点击运行。
首次运行该示例时,它会提示您授予访问权限:
- 点击查看权限。
- 选择账号。
- 点击允许。
脚本的执行日志显示在窗口底部。