【问题标题】:How do I access the G Suite Directory API from App Engine Python?如何从 App Engine Python 访问 G Suite Directory API?
【发布时间】:2020-09-08 18:07:16
【问题描述】:

我正在尝试遵循文档,但我一直无法真正获得访问权限。理想情况下,我想使用应用引擎服务帐户来访问我的域的用户列表,但我愿意使用单独的服务帐户。

  1. 我创建了一个服务帐户。
    电子邮件: myapp-gsuite-prod@mydomain-myapp-3.iam.gserviceaccount.com
    ID: 1097...8840

  2. 服务帐号已启用 G Suite 域范围委派。
    客户 ID: 1097...8840

  3. 在我的域中的安全 > API 控制 > 域范围委派下,我添加了此服务帐户。
    客户端 ID: 1097...8840
    范围:强>https://www.googleapis.com/auth/admin.directory.user.readonly


尝试使用隐式服务帐户

我正在尝试这种方法,因为我希望我可以使用同样的方法来访问 App Engine 服务帐户。

$ export GOOGLE_APPLICATION_CREDENTIALS="/path/to/key.json"
$ python3
>>> import googleapiclient.discovery 
>>> userapi = googleapiclient.discovery.build('admin', 'directory_v1')
>>> userapi.users().list(domain='my-domain.com', maxResults=100).execute()

googleapiclient.errors.HttpError: <HttpError 403 when requesting 
https://www.googleapis.com/admin/directory/v1/users?domain=my-domain.com.com&maxResults=100&alt=json
returned "Not Authorized to access this resource/api">

尝试使用显式服务凭据

这里我实际上是在加载凭据,而不是依赖 googleapiclient.discovery.build 从环境中推断它们:

$ python3
>>> SCOPES = ['https://www.googleapis.com/auth/admin.directory.user.readonly']
>>> SERVICE_ACCOUNT_FILE = '/path/to/key.json'
>>> from google.oauth2.service_account import Credentials
>>> import googleapiclient.discovery 
>>> credentials = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
>>> userapi = googleapiclient.discovery.build('admin', 'directory_v1', credentials=credentials)
>>> userapi.users().list(domain='my-domain.com', maxResults=100).execute()

googleapiclient.errors.HttpError: <HttpError 403 when requesting 
https://www.googleapis.com/admin/directory/v1/users?domain=my-domain.com.com&maxResults=100&alt=json
returned "Not Authorized to access this resource/api">

我也尝试过不使用 domain='my-domain.com 位。这给了我这个错误:

googleapiclient.errors.HttpError: <HttpError 400 when requesting 
https://www.googleapis.com/admin/directory/v1/users?maxResults=100&alt=json 
returned "Bad Request">

所以我很确定请求正在到达 API。我不清楚访问被拒绝的原因。

【问题讨论】:

  • 嗨@Paul 考虑到这种类似的情况here,似乎服务帐户需要角色https://www.googleapis.com/auth/admin.directory.user.security。您能否尝试将此角色分配给服务帐户?
  • 请注意您必须add scope googleapis.com/auth/admin。也可以参考Using OAuth 2.0 to Access Google APIs的概念和解释。
  • @gso_gabriel 在客户端和 G Suite 管理员中添加该范围无济于事。为了畅通无阻,我开始以超级管理员的身份发送主题,这给了我需要的数据。我阅读了您链接的问题,发现这是似乎对大多数人都有效的解决方案,但这仍然感觉像是一个错误。
  • 当然,我明白了。我相信这就是为什么 Github 上的问题仍然存在,以便继续调查/协助的原因。你介意我发布我提供的链接和说明作为答案,包括你做了什么吗?您也可以自己发帖,以便我们对类似案例有答案以帮助社区。​​span>
  • 当然,没关系

标签: python google-app-engine google-workspace service-accounts


【解决方案1】:

将其发布为社区 Wiki,因为它基于 cmets。

为解决此问题,开发人员仍在调查/检查该问题 - 此 Github 问题here 中的更多信息 - 您需要将您的服务帐户添加到更多角色,才能访问 G Suite 目录来自 App Engine Python 的 API。您将需要以超级管理员的身份开始发送主题,这样您就不会再面临权限问题了。这是基于链接问题的 cmets。

【讨论】:

    猜你喜欢
    • 2017-06-16
    • 2020-12-03
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多