创建新用户Google admin sdk python

2024-04-25 06:38:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到一个错误,google无法识别json中的用户名。我找不到包含http头应用程序/json的方法。返回的URL给了我另一个错误代码,但是我通过了登录身份验证。在

我的代码:

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import requests
import json
import pprint

# Setup the Admin SDK Directory API
SCOPES = 'https://www.googleapis.com/auth/admin.directory.user'
#Global scope for access to all user and user alias operations.
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('admin', 'directory_v1', http=creds.authorize(Http()))
# Call the Admin SDK Directory API
userInfo = {
    "name" : {
        "givenName" : "Donald",
        "familyName" : "Dump",
    },
    "kind" : "user",
    "primaryEmail" : "testUser@test.com",
    "isAdmin": False,
    "isDelegatedAdmin": False,
    "agreedToTerms": True,
    "password": "testpass1234",
    "changePasswordAtNextLogin": True  
}
response = service.users().insert(body=json.dumps(userInfo)).execute()

Error codePositional Wrapper _helpers.pyLink that produced HttpError 400


Tags: thestorefromimportbuildclientjsonhttp
1条回答
网友
1楼 · 发布于 2024-04-25 06:38:34

我没有使用googleapi的经验,但我相信它需要一个有效的JSON,所以我们甚至在发送请求之前就在代码中遇到了问题。 因此,在继续之前,您应该修复声明的字典: 写"isAdmin": "false",是不好的,因为True/False是布尔值,但是您将它们作为字符串发送。 您应该改为写"isAdmin": False,。如果编辑json文件本身,请写false,因为在python中布尔值以大写字母开头,而不是json。在

相关问题 更多 >