-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathaddMember.py
65 lines (47 loc) · 1.63 KB
/
addMember.py
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
#! /usr/local/bin/python3
# See /root/root-work/auto-list-update for group name query script and README
import json
import os
import sys
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Scopes required for accessing Google Groups (Cloud Identity API)
SCOPES = ["https://www.googleapis.com/auth/cloud-identity.groups"]
# Path to the Service Account JSON key file
SERVICE_ACCOUNT_FILE = "/webserver/csua-backend/csua-rso-mail-manager-a2ae498a619c.json"
# The google group's ID, found by using list users with the google group email
# this is the ID for: [email protected]
GROUP_NAME = "groups/01tuee740igmbim"
def authenticate():
# Authenticate using a service account
creds = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES
)
return creds
def create_service():
creds = authenticate()
service = build("cloudidentity", "v1", credentials=creds)
return service
def create_google_group_membership(service, member_key):
try:
membership = {
"preferredMemberKey": {"id": member_key},
"roles": [{"name": "MEMBER"}],
}
response = (
service.groups()
.memberships()
.create(parent=GROUP_NAME, body=membership)
.execute()
)
print(response)
except Exception as e:
print(e)
def add_member(email):
# Add an email to the Google Group
service = create_service()
create_google_group_membership(service, email)
def main():
add_member(sys.argv[1])
if __name__ == "__main__":
sys.exit(main())