【问题标题】:403 error from Google Admin SDK with AppAssertionCredentials来自带有 AppAssertionCredentials 的 Google Admin SDK 的 403 错误
【发布时间】:2017-07-03 18:00:35
【问题描述】:

我正在尝试通过 Google 管理目录 API 列出用户。

import logging
import os

from google.appengine.api import memcache
from googleapiclient import discovery
from oauth2client.contrib.appengine import AppAssertionCredentials

import httplib2

from flask import Flask


credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/admin.directory.user')
auth_http = credentials.authorize(httplib2.Http(cache=memcache))
service = discovery.build('admin', 'directory_v1', http=auth_http)    

@app.route('/list')
def list():
    results = service.users().list(domain='example.com', maxResults=10, orderBy='email').execute()
    return 'success'

app = Flask(__name__)

我正在 App Engine 中运行此程序,并按照https://developers.google.com/api-client-library/python/auth/service-accounts 中的说明为 App Engine 默认服务帐户启用了域范围委派

这是我得到的错误:HttpError: https://www.googleapis.com/admin/directory/v1/users?orderBy=email&domain=example.com&alt=json&maxResults=10 返回“未授权访问此资源/api">

【问题讨论】:

  • 正如@noogui 指出的那样,通过重新执行Domain-Wide Delegation 的每个步骤来解决这个问题。如果您能够解决您的问题,我要求您提供答案或选择 noogui 的答案作为解决方案。如果您仍有问题,请提供有关当前域范围委派设置的更多详细信息。
  • 所以您不能使用 App Engine 默认服务帐户?

标签: python google-app-engine google-admin-sdk google-directory-api


【解决方案1】:

按照Delegating domain-wide authority to the service account中指示的步骤:

然后,G Suite 域的管理员必须完成以下步骤:

  1. 转到您 G Suite 域的管理控制台。
  2. 从控件列表中选择安全。如果您没有看到列出的安全性,请从页面底部的灰色栏中选择更多控件,然后从控件列表中选择安全性。如果您看不到控件,请确保您以域管理员身份登录。
  3. 从选项列表中选择显示更多,然后选择高级设置。
  4. 在身份验证部分选择管理 API 客户端访问。
  5. 在“客户名称”字段中输入服务帐户的客户 ID。您可以在“服务帐户”页面中找到您的服务帐户的客户端 ID。
  6. 在一个或多个 API 范围字段中,输入应授予您的应用程序访问权限的范围列表。例如,如果您的应用程序需要在域范围内访问 Google Drive API 和 Google Calendar API,请输入:https://www.googleapis.com/auth/drivehttps://www.googleapis.com/auth/calendar
  7. 点击授权。

确保您的服务帐户设置为管理员。

【讨论】:

  • 如何将服务帐户设置为管理员?是通过 admin.google.com 吗?如果是,我没有办法做到这一点
  • 试试这个。转到您的 Google 开发控制台,转到您的服务帐户,然后单击角色将其​​设置为所有者。
  • 导航到 IAM 控制台并将角色设置为所有者,但仍然收到 403 错误。 HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?orderBy=email&domain=example.com&alt=json&maxResults=10 returned "Not Authorized to access this resource/api
  • 你能试试这个SO thread提供的解决方案吗。
  • 您好,我尝试让 App Engine 默认服务帐户冒充域管理员,但仍然收到相同的 403 错误(见上文)。 credentials = AppAssertionCredentials(scope='https://www.googleapis.com/auth/admin.directory.user', **sub='admin@example.com'**) 我想使用 AppAssertionCredentials,因为如果应用程序已经在 App Engine 上运行,我不需要生成/存储密钥文件,如此处所述 - developers.google.com/identity/protocols/…
猜你喜欢
  • 1970-01-01
  • 2018-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
相关资源
最近更新 更多