问题描述
我已经开始开发一些API,以在我的G Suite目录中创建用户.我遵循了服务帐户教程和 Python的目录教程.我拥有的代码非常简单,只需测试一下它将如何工作即可.
I have started to develop some apis to create users in my G suite directory.I followed the service account tutorials along with the Directory tutorials for python. The code I have is very simple just to test out how it will work.
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)
对我来说,有关大多数事情的文档尚不清楚.当我运行它时,我得到
The documentation to me is unclear about most things. When I run this I get
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
并创建了服务帐户,并下载了服务帐户文件.我错过了一步吗?如果有人拥有我找不到的更好的文档,我也希望这样做.
When I change customer from my_customer
to something else I get Invalid Input
.Any suggestions to what may cause this error and preferably how to work with this api via a service account?Now I did enable the directory api
and create the service account and downloaded the service account file as well. Am I missing a step?I would also prefer if someone has a better documentation that I was unable to find.
推荐答案
确保服务帐户已启用域范围的授权并具有适当的范围.
Ensure that the service account has domain-wide-delegation enabled and has the proper scopes.
这篇关于目录api的gsuite服务帐户返回http 400错误:错误的请求/无效的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!