· 6 years ago · Apr 15, 2020, 02:28 AM
1
2#google stuff
3import datetime
4from googleapiclient.discovery import build
5from googleapiclient.errors import HttpError
6from google.oauth2 import service_account
7
8#placeholder manifest until a manifest can be sent to this script from core
9manifest = {
10 "tenant_id": "reciprocity_ppd",
11 "log_level": "info",
12 "recordtype": "Users",
13 "job_type": "read",
14 "core_config": {
15 "core_api_url": "https://reciprocity.saasyan.com.au/api/v1.0/connector/records"
16 },
17 "token": "e5ba6a6e-e287-4d59-94d0-5751a2186dfb",
18 "job_id": 3811,
19 "records": [],
20 "end_of_list": 1,
21 "job_status": "success",
22 "connector_config": {
23 "client_id": {
24 "type": "string",
25 "value": "111232198740383522653",
26 "required": "true",
27 "description": "Client ID"
28 },
29 "project_id": {
30 "type": "string",
31 "value": "gsuitev3connector",
32 "required": "true",
33 "description": "Project ID"
34 },
35 "private_key": {
36 "type": "string",
37 "value": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCehCXlcNODbNtm\nRxkOOJ+NmjwwSaf8olaqnGcitecn9rVJP+uzjmwmmKoyNLw2SMT9JVWXamoM9YGs\nwsgUk+zM+H4ItL2iJBECLyZNF0mmEY2uqvZJELgQhttj2y4lUrwZT/eo1VLGPz6f\nJz9ZC49+/IDvghyddtTsCiBqkgEn1DtTHmSNcz1CZnicbRwBwA53GqGOGpY/Beeu\n2eMYllU0Bb/Dd5ketQeRUGwCgK4C4euLPyX4FeGVn5h7StIh1HMOSPQSxFTJdxSw\nDop8qEJcgm75ckzPpbMzBYL2QaPK/brVFAPKOXFQJae7LvB98Hi+phQ/8DeF1zVp\n48oHykN3AgMBAAECggEAAox+TbjQfBCA6iumC+627LlKKD5jZKekBuJDJH+8t1St\nFnHDyObhzIX3AUiMPGpTklIDXBYjSC2d9fGfZRVPYP4fqKwtiU1xYXVjSn58CDuP\nIlEwFX2QqeGWkQwHZCTo3OvGeQDxjDoOVJqSuqGWSfo0hPq00xUg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3oXJQkkLrs=\n-----END PRIVATE KEY-----\n",
38 "required": "true",
39 "description": "Private Key"
40 },
41 "client_email": {
42 "type": "string",
43 "value": "service@gsuitev3connector.iam.gserviceaccount.com",
44 "required": "true",
45 "description": "Client Email"
46 },
47 "private_key_id": {
48 "type": "integer",
49 "value": "c68bdcc64d07a7fc82121fb57a1bc27d620364de",
50 "required": "true",
51 "description": "Project Key ID"
52 }
53 },
54 "job_parameters": {
55 "field_mapping": {
56 "kind": "kind",
57 "id": "id",
58 "status": "status",
59 "summary": "summary",
60 "description": "description",
61 "location": "location",
62 "organizer": "organizer",
63 "start": "start",
64 "end": "end"
65 },
66 "time_zone": "Australia/Sydney",
67 },
68 "connector_id": "gsuite_v3_api_native_generic"
69}
70
71
72#a config object has to be formatted differently to how it is formatted in the manifest for google authentication to work
73config = {}
74
75#firstly all dynamic fields are copied from manifest object to the new config object
76config["client_id"] = manifest['connector_config']['client_id']['value']
77config["project_id"] = manifest['connector_config']['project_id']['value']
78config["private_key"] = manifest['connector_config']['private_key']['value']
79config["client_email"] = manifest['connector_config']['client_email']['value']
80config["private_key_id"] = manifest['connector_config']['private_key_id']['value']
81
82#then all static fields are hardcoded into the config object
83config['auth_uri'] = "https://accounts.google.com/o/oauth2/auth"
84config['token_uri'] = "https://oauth2.googleapis.com/token"
85config['auth_provider_x509_cert_url'] = "https://www.googleapis.com/oauth2/v1/certs"
86config['client_x509_cert_url'] = "https://www.googleapis.com/robot/v1/metadata/x509/service%40gsuitev3connector.iam.gserviceaccount.com"
87config['type'] = "service_account"
88
89
90#levels of access needed - currently /calendar gives read + write + manage access
91SCOPES = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/admin.directory.user']
92
93
94#creating credentials object
95creds = service_account.Credentials.from_service_account_info(
96 config, scopes=SCOPES)
97
98domain = 'iteralis.com.au'
99
100#building api service - authentication occurs here
101service = build('admin', 'directory_v1', credentials=creds)
102
103results = service.users().list(domain=domain, maxResults=10,
104 orderBy='email').execute()
105print(results)