【问题标题】:Received error "Not Authorized to access this resource/api" when trying to use Google Directory API and Service Account Authentication尝试使用 Google Directory API 和服务帐户身份验证时收到错误“未授权访问此资源/api”
【发布时间】:2013-09-13 10:05:37
【问题描述】:

我真的很难尝试使用服务帐户身份验证来使用 Google Directory API(Admin SDK)。

使用基于客户端的三足 OAuth 可以正常工作(在此处测试 - https://developers.google.com/admin-sdk/directory/v1/reference/members/insert),但我正在使用的服务帐户的权限委派存在问题。在 Google Apps 管理下,我启用了使用 API 并按照指示将服务帐户添加到允许的 OAuth 客户端列表中。

代码如下:

import httplib2
import sys

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

credentials = SignedJwtAssertionCredentials(
    '<KEY>@developer.gserviceaccount.com',
    '<KEY DATA>',
    scope='https://www.googleapis.com/auth/apps.groups.settings https://www.googleapis.com/auth/admin.directory.group https://www.googleapis.com/auth/admin.directory.group.member'
)
http = httplib2.Http()
http = credentials.authorize(http)

service = build("admin", "directory_v1", http=http)
groups = service.groups()
g = groups.get(groupKey="<GROUP NAME>").execute()

最终,我收到以下错误:

apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/groups/<GROUP NAME>?alt=json returned "Not Authorized to access this resource/api">

我也尝试使用以下 API:

service = build("groupssettings", "v1", http=http)

但这也会返回一个错误 - “后端错误”。

【问题讨论】:

    标签: python google-apps google-admin-sdk


    【解决方案1】:

    即使您使用的是服务帐户,您仍然需要代表实例中具有适当管理员权限的 Google Apps 用户执行操作。尝试做:

    credentials = SignedJwtAssertionCredentials(
      '<KEY>@developer.gserviceaccount.com',
      '<KEY DATA>',
      scope='https://www.googleapis.com/auth/apps.groups.settings https://www.googleapis.com/auth/admin.directory.group https://www.googleapis.com/auth/admin.directory.group.member',
      sub='super-admin@yourdomain.com'
    )
    

    其中 super-admin@yourdomain.com 是您 Google Apps 帐户的超级管理员。

    【讨论】:

    • 谢谢,成功了!你是怎么知道这个解决方案的?我知道这种类型的解决方案是我正在寻找的解决方案,但我找不到相关文档。
    • 这个 sub='super-admin@yourdomain.com' 让它工作!
    • 我们还使用 'super-admin@yourdomain.com' 作为 serviceAccountUser 吗?似乎只有 'admin@yourdomain.com' 是合法的。
    • 但这意味着什么?即使在 2017 年我也无法弄清楚:我只是想了解 我的 组织,为什么这需要域管理员电子邮件?没有那种电子邮件地址,如何获得这些信息?
    猜你喜欢
    • 2021-03-29
    • 1970-01-01
    • 2020-06-01
    • 2017-03-01
    • 1970-01-01
    • 2019-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多