跳到主内容

添加 Google 作为 OAuth 提供商

本指南说明如何允许用户使用其 Google 账号登录 Casdoor。


学习成果​

  • 在 Google Cloud 中创建一个 OAuth 客户端。
  • 在 Casdoor 中添加 Google 作为 OAuth 提供商。
  • 从 Google 读取用户的电话号码。

所需条件​

  • 一个 Google Cloud 项目。参见 Google API Console。
  • Casdoor 管理控制台的管理员访问权限

在 Google Cloud 中创建 OAuth 客户端​

  1. 创建或选择一个项目。

    New project in Google Cloud

  2. 转到 APIs & Services > OAuth consent screen 并配置同意屏幕。

    OAuth consent screen

    App registration

  3. 转到 Credentials,点击 Create credentials,然后选择 OAuth client ID。

    Credentials page

  4. 选择应用程序类型,例如 Web application,并将 Casdoor 的回调 URL https://<your-casdoor-host>/callback 添加到 Authorized redirect URIs。这是 Casdoor 的 URL,而不是你自己应用的 URL。参见 Redirect URL and callback URL。

    OAuth client creation form

  5. 创建客户端并复制 Client ID 和 Client secret。

    Client ID and client secret

在 Casdoor 中添加提供商​

  1. 在 Casdoor 管理控制台中,转到 Identity > Providers 并添加一个提供商。

  2. 将 Category 设置为 OAuth,将 Type 设置为 Google。

  3. 输入 Client ID 和 Client secret。

    Google provider in Casdoor

  4. 保存提供商.

读取用户电话号码​

Google 仅在包含额外 scope 时返回电话号码。例如,当你开启 Get password 时需要它。

  1. 在 Google Cloud 中,启用 Google People API。

    Google People API

  2. 在 Casdoor 中,将 scope https://www.googleapis.com/auth/user.phonenumbers.read 添加到该提供商。

    Scope field of the Google provider

下一步​

另请参阅​