首页 > 解决方案 > google.auth.exceptions.RefreshError: ('access_denied: Account restricted' using a domain-wide delegated account

问题描述

我正在尝试在服务帐户上使用域范围的委派从管理 sdk 获取一些信息以生成报告。我有一个已经在工作的代码库,我将它与 gsuite 域一起使用。我想使用相同的代码为另一个域生成报告,所以我为这个另一个域设置了一个 GCP 项目。我创建了一个服务帐户,在其上启用了域范围的委派,并启用了服务帐户的范围。当我尝试进行任何 api 调用模拟域上的任何帐户时,我收到以下错误

adminService.activities().list(userKey='all', applicationName='meet').execute()
google.auth.exceptions.RefreshError: ('access_denied: Account restricted', '{\n  "error": "access_denied",\n  "error_description": "Account restricted",\n  "error_uri":<url>}')

这是我用来创建服务的代码

credentials = service_account.Credentials.from_service_account_file(credentialsPath,
    scopes=['https://www.googleapis.com/auth/admin.reports.audit.readonly',
    "https://www.googleapis.com/auth/classroom.announcements.readonly",
    "https://www.googleapis.com/auth/classroom.courses.readonly",
    "https://www.googleapis.com/auth/classroom.coursework.students.readonly",
    "https://www.googleapis.com/auth/classroom.profile.emails",
    "https://www.googleapis.com/auth/classroom.rosters.readonly",
    "https://www.googleapis.com/auth/classroom.student-submissions.students.readonly",
    "https://www.googleapis.com/auth/classroom.topics.readonly"
    ])

    delegated_credentials = credentials.with_subject(email)

    return build('admin', 'reports_v1', credentials=delegated_credentials)

同样,这不应该是一个编程问题,因为相同的确切代码适用于另一个域,我认为我在 admin/GCP 配置方面遗漏了一些东西,但我不知道是什么,我还没有在互联网上的任何地方都发现了这个确切的错误

标签: pythongoogle-admin-sdk

解决方案


事实证明,要使域范围的委派工作,您必须启用此功能

这大致翻译为创建 GCP 项目的用户所属的组织单元上的“没有个人控制的附加服务”。您可以通过管理面板中“应用程序”>“其他 Google 服务”页面顶部的工具提示进行操作


推荐阅读