-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusersInsert.py
More file actions
32 lines (25 loc) · 1.25 KB
/
usersInsert.py
File metadata and controls
32 lines (25 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import json
from google.oauth2 import service_account
import googleapiclient.discovery
# Scopes required by this endpoint -> https://developers.google.com/admin-sdk/directory/reference/rest/v1/users/insert
SCOPES = ['https://www.googleapis.com/auth/admin.directory.user']
# Service Account Credentials to be used. How to create at https://developers.google.com/workspace/guides/create-credentials#service-account
SERVICE_ACCOUNT_FILE = 'SERVICE_ACCOUNT_CREDENTIALS.json'
# Super Admin account to be impersonated by the Service account created
DELEGATE = 'SUPERADMIN@YOURDOMAIN.COM'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
credentials_delegated = credentials.with_subject(DELEGATE)
service = googleapiclient.discovery.build(
'admin', 'directory_v1', credentials=credentials_delegated)
# JSON template to be used for user creation. Additional fields can be found at https://developers.google.com/admin-sdk/directory/reference/rest/v1/users#User
newUserInfo = {
"name": {
"familyName": "Doe",
"givenName": "John"
},
"password": "A_SECURE_PASSWORD**",
"primaryEmail": "USERNAME@YOURDOMAIN.COM",
}
response = service.users().insert(body=newUserInfo).execute()
print(response)