为Google Directory API (Python)添加多用户JSON



当我尝试添加用户:这样我只能添加一个用户,但是我需要从JSON文件中添加多个用户。

req =  [
{
"primaryEmail": "first.user@mydomain.com",
"name": {
"givenName": "UserName",
"familyName": "UserLastName"
},
"suspended": False,
"password": "my password",
"hashFunction": "SHA-1",
"changePasswordAtNextLogin": False,
"agreedToTerms": True,
"ipWhitelisted": False                
},
{
"primaryEmail": "second.user@exnesstest.com",
"name": {
"givenName": "FirstName",
"familyName": "LastName"
},
"suspended": False,
"password": "my password",
"hashFunction": "SHA-1",
"changePasswordAtNextLogin": False,
"agreedToTerms": True,
"ipWhitelisted": False               
}
]
users = service.users().insert(body=req).execute()

得到如下错误:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://admin.googleapis.com/admin/directory/v1/users?alt=json returned "Invalid Input: primary_user_email">

如何以这种方式添加多个用户?

如果您想在一个API调用中发送多个操作,则必须使用BatchRequest。下面是google

提供的Python文档中的一个示例
def list_animals(request_id, response, exception):
if exception is not None:
# Do something with the exception
pass
else:
# Do something with the response
pass
def list_farmers(request_id, response):
"""Do something with the farmers list response."""
pass
service = build('farm', 'v2')
batch = service.new_batch_http_request()
batch.add(service.animals().list(), callback=list_animals)
batch.add(service.farmers().list(), callback=list_farmers)
batch.execute()

您可以在这里阅读更多关于该方法的信息:https://developers.google.com/admin-sdk/directory/v1/guides/batch

或者直接使用github存储库(示例来自哪里):https://googleapis.github.io/google-api-python-client/docs/batch.html

你唯一需要知道的是每秒/分钟/小时发送请求的阈值,但如果你超过限制,谷歌会出错,你将不得不使用指数后退限制请求

相关内容

  • 没有找到相关文章