Connect Gmail to Odoo using Google OAuth

Odoo 兼容 Google 的 OAuth 用于 Gmail。为了从自定义域发送安全电子邮件,只需要在 Google 的 Workspace 平台上配置一些设置,以及在 Odoo 数据库的后端上进行配置。此配置可通过使用个人电子邮件地址或自定义域创建的地址来实现。

小技巧

For more information, visit Google’s documentation on setting up OAuth.

在 Google 中设置

Create a new project

要开始,请转到 Google API Console。使用您的 Google Workspace 帐户登录(如果您有一个),否则使用您的个人 Gmail 帐户登录(这应该与您要在 Odoo 中配置的电子邮件地址匹配)。

After that, click on 创建项目, located on the far right of the OAuth consent screen. If a project has already been created in this account, then the New Project option will be located on the top right under the Select a project drop-down menu.

New Project 屏幕上,将 Project name 重命名为 Odoo 并浏览 Location。将 Location 设置为 Google Workspace organization。如果您使用的是个人 Gmail 帐户,则将 Location 保留为 No Organization

Google OAuth 的项目名称和位置。

Click on Create to finish this step.

编辑应用程序注册

接下来,我们将配置项目的应用程序注册。

OAuth consent screen 步骤中,在 App information 部分,输入 OdooApp name 字段中。在 User support email 字段下选择组织的电子邮件地址。

接下来,在 应用程序域 ‣ 授权域 下,点击 添加域名 并输入 odoo.com

之后,在 开发者联系信息 部分,输入组织的电子邮件地址。Google 使用此电子邮件地址通知组织有关项目的任何更改。

Next, click on the Save and Continue button. Then, skip the Scopes page by scrolling to the bottom and clicking on Save and Continue.

If continuing in testing mode (External), add the email addresses being configured under the Test users step, by clicking on Add Users, and then the Save and Continue button. A summary of the app registration appears.

Finally, scroll to the bottom and click on Back to Dashboard to finish setting up the project.

Create Credentials

现在项目已经设置好,应该创建凭据,其中包括 Client IDClient Secret。首先,在左侧边栏菜单中点击 Credentials

Then, click on Create Credentials in the top menu and select OAuth client ID from the dropdown menu.

  • Application Type 下,从下拉菜单中选择 Web Application

  • In the Name field, enter Odoo.

  • 授权重定向 URI 标签下,点击 添加 URI 按钮,然后在 URI 1 字段中输入 https://yourdbname.odoo.com/google_gmail/confirm。请确保将 URL 中的 yourdbname 部分替换为实际的 Odoo 数据库名称。

  • 接下来,点击 创建 以生成 OAuth 客户端ID客户端密钥。最后,复制每个生成的值以备在配置Odoo时使用,然后导航到Odoo数据库。

Client ID and Client Secret for Google OAuth.

在Odoo中设置

Enter Google Credentials

首先,打开 Odoo 并导航到 Apps 模块。然后,从搜索栏中移除 Apps 过滤器,并输入 Google。安装名为 Google Gmail 的模块。

接下来,导航到 设置 ‣ 通用设置,在 讨论 部分,确保 自定义电子邮件服务器 或者 外部电子邮件服务器 复选框被选中。这将 生成一个新的选项 Gmail 凭据 或者 使用 Gmail 服务器。然后,将 相应的值复制并粘贴到 客户端 ID客户端密钥 字段中,并 保存 设置。

Configure outgoing email server

要配置外部 Gmail 帐户,请返回到 自定义电子邮件服务器 设置的顶部,然后点击 发送电子邮件服务器 链接。

Configure Outgoing Email Servers in Odoo.

然后,点击 NewCreate 来创建一个新的邮件服务器,并填写 NameDescription 和邮件 Username (如果需要)。

接下来,点击 Gmail OAuth 认证 或者 Gmail (在 Authenticate with 或者 Connection 部分下面)。 最后,点击 连接你的 Gmail 账户

打开一个标有 Google 的新窗口以完成授权过程。在Odoo中选择正在配置的适当的电子邮件地址。

如果电子邮件地址是个人帐户,则会弹出一个额外的步骤,所以点击 Continue 允许验证并将 Gmail 帐户连接到 Odoo。

然后,通过点击 Continue 或者 Allow,允许Odoo访问Google账户。之后,页面会导航回Odoo中新配置的外发邮件服务器。配置会自动加载Odoo中的令牌,并且一个标签 Gmail Token Valid 会以绿色出现。

Configure Outgoing Email Servers in Odoo.

最后, 测试连接 。应该会出现一个确认消息。现在,Odoo数据库可以通过使用OAuth身份验证来安全地发送电子邮件。

Google OAuth 常见问题解答

生产环境与测试环境发布状态

选择 生产 作为 发布状态 (而不是 测试) 将显示以下警告消息:

OAuth 仅限于 100 个敏感范围登录。

要纠正此警告,请导航到 Google API 平台。如果 发布状态生产中,请点击 返回测试 来纠正此问题。

未添加测试用户

如果在OAuth同意屏幕中没有添加测试用户,则会出现403访问被拒绝的错误。

403 访问被拒绝错误。

要纠正此错误,请返回到 OAuth consent screen`(在 :guilabel:`APIs & Services 下)并向应用程序添加测试用户。添加您在 Odoo 中配置的电子邮件。

Gmail模块未更新

如果Odoo中的 Google Gmail 模块没有更新到最新版本,则会出现 Forbidden 错误消息。

禁止访问,您没有权限访问所请求的资源。

要纠正此错误,请转到 应用 模块并清除搜索条件。然后,搜索 GmailGoogle 并升级 Google Gmail 模块。最后,点击模块右上角的三个点并选择 升级

应用程序类型

当创建凭证(OAuth Client IDClient Secret)时,如果选择 Desktop App 作为 Application Type,将出现 Authorization Error

Error 400 重定向URI不匹配。

要纠正此错误,请删除已创建的凭据并创建新的凭据,选择: Web Application 作为 Application Type 。然后,在 Authorized redirect URIs 下,点击 ADD URI 并输入: https://yourdbname.odoo.com/google_gmail/confirm ,确保在 URL 中用 Odoo 数据库名称替换 yourdbname