【发布时间】:2020-05-16 02:40:33
【问题描述】:
我的名字是阿德里安。我想通过python3通过google admin SDK将用户添加到G套件。
这是我的问题:
我有这个代码:
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']
def createUserConnection():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token, encoding='Latin-1')
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
createUserConnection.service = build('admin', 'directory_v1', credentials=creds)
def addUser(name,familyName,usermail):
print('Adding user '+usermail+' to the G suite')
#json definition
userInfo = json.dumps({
"name" : {
"givenName": name,
"familyName": familyName
},
"kind": "admin#directory#user",
"primaryEmail": usermail,
"password": "Welcome1234",
"changePasswordAtNextLogin": True
})
createUserConnection.service.users().insert(body=userInfo).execute()
if __name__ == '__main__':
createUserConnection()
addUser("bla","bla","blabla@24i.com")
当我通过 python3 运行它时,它会再次出现错误。文件“/Users/adrianbardossy/Downloads/google_accounts/python3.7/lib/python3.7/site-packages/googleapiclient/http.py”,第 856 行,正在执行
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/admin/directory/v1/users?alt=json returned "Invalid Input: primary_user_email">
我试图通过传递用户名 blabla 而不是 blabla@24i.com 来解决,但仍然是同样的问题。基于此处的文档:https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/python/latest/admin_directory_v1.users.html 用于插入方法。你能帮我解决这个问题吗?
阿德里安
【问题讨论】:
-
你好,你能帮我吗?它在这里卡了 2 周
标签: python-3.x google-workspace