-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_api.py
More file actions
65 lines (54 loc) · 2.18 KB
/
google_api.py
File metadata and controls
65 lines (54 loc) · 2.18 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.service_account import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from user import User
SCOPES = [
"https://www.googleapis.com/auth/admin.directory.user",
"https://www.googleapis.com/auth/admin.directory.group.member",
]
class GoogleAPI:
def __init__(self, config_name: str) -> None:
creds = None
if os.path.exists(config_name):
creds = Credentials.from_service_account_file(config_name, scopes=SCOPES)
self.service = build("admin", "directory_v1", credentials=creds)
def create_new_account(self, user: User) -> str:
info = {
"primaryEmail": user.email,
"name": {
"givenName": user.given_name,
"familyName": user.family_name,
},
"password": user.password,
"changePasswordAtNextLogin": True,
"recoveryEmail": user.recovery_email,
}
try:
# Try to create new account
return self.service.users().insert(body=info).execute()
except Exception as e:
if "Entity already exists" in str(e):
# Account exists, try to update it
update_info = info.copy()
del update_info['primaryEmail'] # Can't update primary email
try:
return self.service.users().update(userKey=user.email, body=update_info).execute()
except Exception as update_e:
raise update_e
raise e
def add_account_to_group(
self, groupkey: str, email: str
) -> str: # group key e.g email or id of group
payload = {
"email": email,
}
try:
return self.service.members().insert(groupKey=groupkey, body=payload).execute()
except Exception as e:
if "Member already exists" in str(e):
# Member is already in the group, this is fine
return f"User {email} is already a member of {groupkey}"
raise e