[英]Creating new User Google admin sdk 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()
我對 Google API 沒有經驗,但我相信它需要一個有效的 JSON,所以即使在發送請求本身之前,我們的代碼也有問題。 所以,在繼續之前,你應該修正你聲明的字典:寫"isAdmin": "false",
是不行的,因為 True/False 是布爾值,但是你將它們作為字符串發送。 你應該寫"isAdmin": False,
而不是。 如果編輯 json 文件本身,請寫false
,因為在 python 中布爾值以大寫字母開頭,但在 JSON 中不是。
讓我添加我的 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
。 我的意思是它是用戶密鑰。
默認情況下忽略有效負載用戶插入中的isAdmin
。 要創建用戶管理員,您應該使用makeAdmin
方法傳遞 userKey(用戶電子郵件有效)和 status:true 的有效負載。
您可以在下方找到所有 API 文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.