【问题标题】:Creating new User Google admin sdk python创建新用户 Google admin sdk python
【发布时间】:2018-06-20 16:05:45
【问题描述】:

我收到一个错误,即 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()

【问题讨论】:

  • 更新了布尔值,现在来自 googleapiclient 的 positional_wrapper 出现新错误
  • 您可以不使用图像,而是复制文本并在此处发布更新的错误吗?顺便说一句,positional_wrapper 错误是什么?
  • 您是如何解决“Invalid Given/Family Name: FamilyName”错误的?

标签: python google-admin-sdk google-directory-api


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    让我添加我的 50cc。 这段代码对我有用。

    admin_user_payload = {
     "status": "true"
     } 
    
    request = service.users().makeAdmin(userKey='admin_user_key', body=admin_user_payload)
    admin = request.execute()
    

    其中admin_user_key 应该是例如info@myaccount.com。我的意思是它是 userKey。

    默认情况下忽略有效负载用户插入中的isAdmin。要使用户成为管理员,您应该使用 makeAdmin 方法传递 userKey(用户电子邮件有效)和 status:true 的有效负载。

    您可以在下面找到所有 API 文档。

    https://google-api-client-libraries.appspot.com/documentation/admin/directory_v1/python/latest/admin_directory_v1.users.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 2014-11-20
      • 1970-01-01
      • 2017-01-05
      • 2021-06-22
      • 1970-01-01
      • 2014-10-12
      相关资源
      最近更新 更多