【问题标题】:gsuite service account for directory api returns http 400 errors: Bad request/Invalid input目录 api 的 gsuite 服务帐户返回 http 400 错误:错误请求/无效输入
【发布时间】:2020-05-10 19:28:21
【问题描述】:

我已经开始开发一些 api 来在我的 G Suite 目录中创建用户。 我关注了service account tutorials 以及用于python 的Directory tutorials。我的代码非常简单,只是为了测试它是如何工作的。

from google.oauth2 import service_account
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']
SERVICE_ACCOUNT_FILE = 'file'
creds = service_account.Credentials.from_service_account_file(
    SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('admin', 'directory_v1', credentials=creds)
results = service.users().list(customer='i am not sure what customer is', maxResults=10, orderBy='email').execute() 
#this line produces the error.
#Vscode also states the service has no member users. But I did install all #the libraries
users = results.get('users', [])
print(users)

对我来说,大多数事情的文档都不清楚。当我运行它时,我得到了

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

当我将客户从 my_customer 更改为其他名称时,我得到了 Invalid Input。 关于可能导致此错误的原因以及如何通过服务帐户使用此 api 的任何建议? 现在我确实启用了directory api 并创建了服务帐户并下载了服务帐户文件。我错过了一步吗? 如果有人有我找不到的更好的文档,我也更愿意。

【问题讨论】:

  • 嗨,@sakib11。我在使用 Java 时遇到同样的问题,您有解决方案吗?
  • 好的,所以问题与范围有关。从 Gsuite 管理部门重新检查您的范围,并确保客户可以是“我”。
  • 所以我重新审视了这个,这只是一个范围问题。只需转到您的谷歌管理员安全页面并确保您的委托服务帐户具有所需的范围。
  • 是的,它与作用域有关。我还必须模拟一个管理员帐户才能使其工作。谢谢!
  • 帮助很大。

标签: python google-api service-accounts http-error google-directory-api


【解决方案1】:

确保服务帐户启用了域范围的委派并具有适当的范围。

【讨论】:

    【解决方案2】:

    最后我通过在调用“service_account.Credentials.from_service_account_file”时设置另一个参数“subject”解决了这个问题。

    【讨论】:

    • 对我来说,我什至不知道有这样的参数存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多