目录 api 的 gsuite 服务帐户返回 http 400 错误:请求错误/输入无效

sak*_*b11 5 python google-api http-error service-accounts google-directory-api

我已经开始开发一些 api 来在我的 G suite 目录中创建用户。我遵循了服务帐户教程以及python目录教程。我的代码非常简单,只是为了测试它如何工作。

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)
Run Code Online (Sandbox Code Playgroud)

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

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">
Run Code Online (Sandbox Code Playgroud)

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

sak*_*b11 2

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