【问题标题】:How to debug Authentication configuration?如何调试身份验证配置?
【发布时间】:2019-05-06 17:35:22
【问题描述】:

我们在域范围委派的服务帐户身份验证方面遇到问题。 主要问题是很难调查和调试身份验证配置,所以 我们想询问一些如何调试配置的提示。 或者,也许我们缺少一些配置选项,您可以指出它们。

我们的流程是:

  1. 创建启用了域范围委派的 SA(=服务帐户)。
  2. 在 GSuite 管理控制台 (https://support.google.com/a/answer/162106?hl=en) 中验证 SA。
    • 使用凭证文件中的client_id。 (现在是电子邮件)
    • 范围以逗号分隔,中间没有空格。
  3. 确保选中“安全性 > API 参考 > API 参考 -> '启用 API 访问'”。

对于某些 GSuite 域,这是有效的配置,但我们得到了一些域,该配置会导致:

google.auth.exceptions.RefreshError: ('unauthorized_client: 客户端未授权使用此方法检索访问令牌。', '{\n "error": "unauthorized_client",\n "error_description": "客户端未授权使用此方法检索访问令牌。"\n}')

据我们了解,这是表示未将 client_id 和范围添加到“管理 API 客户端访问”页面的错误。 (=经过身份验证的客户端列表)

我们确实确保我们请求的 GSuite 域具有正确的 client_id 和添加到经过身份验证的客户端列表中的范围 + 具有“启用 API 访问”。 我们甚至与他们一起创建了共享桌面并自己完成以完全确定这一点。 但错误仍然存​​在。

但是,我们无法在我们的测试 GSuite 域上复制此问题。 我们使用与客户端相同的 SA 尝试了几个选项:

  1. 模拟帐户没有访问资源的权限。 结果是:

googleapiclient.errors.HttpError: https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&alt=json 返回“未授权访问此资源/api”>

  1. 范围只是部分:

google.auth.exceptions.RefreshError: ('access_denied: 请求的客户端未授权。', '{\n "error": "access_denied",\n "error_description": "请求的客户端未授权。"\n} ')

  1. 未选中“启用 API 访问”。

googleapiclient.errors.HttpError: https://www.googleapis.com/admin/directory/v1/users?customer=my_customer&alt=json 返回“域不能使用 api。”

  1. 我们从客户端收到的错误(“客户端未经授权无法使用此方法检索访问令牌。”),只有当 client_id 根本不在经过身份验证的客户端列表中时,我们才能进行复制。 但我们确信,有问题的 GSuite 域在“管理 API 客户端访问”页面中对 SA 进行了身份验证。

我们正在使用这些范围:https://www.googleapis.com/auth/userinfo.profile,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/gmail.readonly,https://www.googleapis.com/auth/plus.login,https://www.googleapis.com/auth/calendar.readonly,https://www.googleapis.com/auth/contacts.readonly, https://www.googleapis.com/auth/admin.directory.user.readonly

您对如何调试/解决此问题有任何想法吗?

【问题讨论】:

    标签: google-api google-oauth service-accounts google-workspace


    【解决方案1】:

    这是您需要做的。仔细检查每个步骤。如有疑问,请重新开始。

    1. 启用“Admin SDK API。这是在每个项目的基础上启用的。
    2. 创建服务帐户。不要添加或删除任何权限。不要以任何方式更改服务帐户。如果你这样做,你会得到一个错误,你没有被授权。
    3. 在服务帐户上启用域范围委派。
    4. 按照此文档将域范围的权限委派给您的服务帐户:

    Delegate domain-wide authority to your service account

    1. 在创建服务帐户凭据(从下载的 Json)时,您将需要以下范围来进行完整的 G Suite 管理:

      "https://www.googleapis.com/auth/admin.directory.group", "https://www.googleapis.com/auth/admin.directory.user"

    2. 模拟创建新凭据的用户帐户。用户帐号必须是 G Suite 超级管理员。此帐户必须至少登录一次 G Suite 并接受服务条款

    3. 使用第 5 步中的凭据创建您的客户端。

    工作 Python 示例:

    from googleapiclient.discovery import build
    from google.oauth2 import service_account
    
    # This is the service account credentials file
    credentials_file = 'google-directory-api.json'
    
    # In this example I only need to send email
    credentials = service_account.Credentials.from_service_account_file(
                    credentials_file,
                    scopes=['https://www.googleapis.com/auth/gmail.send'])
    
    # This user is a G Suite superadmin
    impersonate = 'username@example.com'
    
    credentials = credentials.with_subject(impersonate)
    
    service = build('gmail', 'v1', credentials=credentials)
    

    【讨论】:

      【解决方案2】:

      我认为我们需要分阶段进行。让我们从您的第一个错误开始,看看是否能解决您的问题。

      背景信息

      您可以在 Google 开发者控制台中创建多种类型的客户端。以下是前三名。

      • 浏览器客户端:用于 Web 应用程序
      • 本机客户端(其他):用于已安装的桌面应用程序
      • 服务帐号:用于服务器到服务器的通信。

      客户端不同,你下载的client.json文件不同,用于认证服务器的验证码也不同。

      错误 1:代码/客户端不匹配

      unauthorized_client:客户端未授权使用此方法检索访问令牌。

      可以表示两件事之一。您已经创建了一个服务帐户客户端并且您没有使用服务帐户代码进行身份验证,或者您正在使用该代码对服务帐户进行身份验证但您尚未创建服务帐户客户端。您尚未发布您使用的语言或任何代码,因此我无法告诉您您使用的代码是否旨在与服务帐户一起使用。您将不得不稍微研究一下。

      检查开发者控制台,确保您的客户端是这样的,如果是,请检查您的代码。如果不是,则创建一个真实的服务帐户客户端,然后重试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-14
        • 2015-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-24
        相关资源
        最近更新 更多