
Gmail获取Client ID和Client Secret
在当今数字化时代,Gmail 作为一款广泛使用的电子邮件服务,其相关的技术操作对于开发者和高级用户至关重要。本文将聚焦于在 Gmail获取Client ID和Client Secret这一关键内容,详细阐述获取的步骤、所需条件以及可能遇到的问题,帮助你顺利完成这一流程,无论是用于开发邮件相关应用、集成第三方服务还是其他涉及 Gmail API 的操作。
在您的Google帐户中创建Web应用程序
在新打开的标签页或窗口中,登录您的 Google 帐户并打开Google Cloud Console。
此外,如果您使用 Google Workspace,请确保您使用的 Google 帐户有权发送电子邮件(换句话说,可以访问其自己的收件箱)。
如果这是您第一次使用 Google Cloud Console,您可能会看到一个弹出窗口,要求您选择您的国家/地区并同意服务条款。继续操作,然后单击“同意并继续”以继续下一步。

如果您之前登录过 Google Cloud Console,您可能会绕过服务条款。
创建或选择项目
首先,您需要为您的应用选择一个项目。您可以选择现有项目或创建一个新项目。
为此,请单击仪表板顶部工具栏中的项目下拉菜单。

在出现的弹出窗口中,从列表中选择一个现有项目或单击右上角的新项目。

如果您选择了现有项目,请继续下一步。对于新项目,请输入项目名称并从下拉列表中选择组织和位置。

然后点击创建按钮。
启用 Gmail API
接下来,您需要为项目启用 Gmail API。在 Google Cloud Console 侧栏中,转到API 和服务 » 库。

然后在类别下查看并点击Google Workspace,或在搜索栏中输入“Gmail API”。

点击Gmail API结果。

在 Gmail API 页面上,点击蓝色的ENABLE按钮。
![启用 Gmail API]](https://i0.wp.com/1aha.com/wp-content/uploads/2024/11/enable-gmail-api.png?resize=1024%2C580&ssl=1)
创建应用程序的凭证
启用 Gmail API 后,您将被重定向到 Gmail API 概述页面。在这里,单击创建凭据按钮。

在下一页中,Google 会询问几个问题来确定您需要的凭证类型。从选择 API下拉菜单中,选择Gmail API。

接下来,在“您将访问哪些数据?”下,选择“用户数据”选项。然后单击“下一步”按钮继续。

配置您的 OAuth 授权
然后,Google 会询问有关您的应用的一些基本信息。
此部分主要供个人使用,因为没有其他人会使用您的应用。但是,某些字段仍标记为必填:
- 应用程序名称:输入您选择的应用程序名称(例如,Pattie 的应用程序)。
- 用户支持电子邮件:从提供的选项中选择您的电子邮件地址。
- 应用logo:如果您愿意,可以上传应用logo。这是可选的。

最后,在电子邮件地址字段中添加您的电子邮件地址。然后单击保存并继续按钮继续下一步。

配置范围(可选)
这是可选步骤,本教程中我们将跳过此步骤。向下滚动到“范围”部分的末尾,然后单击“保存并继续”按钮继续。

设置您的 OAuth 客户端 ID
接下来,您需要填写一些有关您的 OAuth 客户端 ID 的信息。
从应用程序类型下拉菜单中,选择Web 应用程序选项。选择后,将自动填充更多字段。

您可以将“名称”字段保留为默认值,也可以将其更改为更相关的内容。在本例中,我们将保留默认名称“Web client 1”。

接下来,跳过“授权的 JavaScript 来源”部分并滚动到“授权的重定向 URI”。
单击+ 添加 URI按钮并输入你的重定向地址,如果你不清楚可以在文章后面留言。

然后点击CREATE按钮来完成此步骤。

创建应用后,“您的凭据”部分将展开以显示您的客户端 ID。现在无需复制它,因为您将在后面的步骤中从其他区域访问它。
相反,继续并单击页面底部的“完成”按钮。

将发布状态从测试更新为生产
Google 会默认将您的应用置于内部模式。将其切换为外部模式并发布非常重要。否则,您的应用将受到极大限制,无法正常运行。
Google Workspace 用户
在您的 Google Cloud Console 侧边栏中,转到API 和服务 » OAuth 同意屏幕。在用户类型下,单击MAKE EXTERNAL按钮。

注意:如果将用户类型设置为内部,则只有组织内的用户才能访问您的应用。例如,如果您的工作区域是example.com,则用户只能使用@example.com电子邮件地址访问您的应用。
在出现的弹出窗口中,选择“生产中”选项。然后点击“确认”。

Gmail 用户
如果您没有使用 Google Workspace,则不会看到 MAKE EXTERNAL 选项。相反,您需要发布您的 Google 应用。
为此,请转到API 和服务 » OAuth 同意屏幕。在发布状态下,您将看到应用程序状态设置为测试。继续并单击发布应用程序按钮以更新您的应用程序状态。

在出现的覆盖中,单击“确认”以发布您的应用。

确认完成后,您将看到应用程序的发布状态现在为“正在生产”。

授予您的网站 Google / Gmail 权限
接下来,点击左侧菜单中的“凭证” 。

进入“凭据”页面后,在OAuth 2.0 客户端 ID部分中,您可以看到刚刚创建的 Web 应用程序的详细信息。要查看客户端 ID和客户端密钥,请单击铅笔图标。

这将打开您应用的所有详细信息。在此页面的右侧,您将看到客户端 ID和客户端密钥值。

至此,Gmail获取Client ID和Client Secret就大功告成了!