-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.gs
112 lines (94 loc) · 3.25 KB
/
Code.gs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
var PRIVATE_KEY = "";
var CLIENT_EMAIL = "";
function syncUsers(){
//set the users that needs to sync the salesforce contact groups with myContacts
var userList = ['[email protected]','[email protected]']
for (var i in userList){
contactSyncSF(userList[i]);
}
}
function contactSyncSF(email) {
var service = getService(email);
service.reset();
var sfGroupId = getSFgroup(service);
if (sfGroupId){
var sfGroupMembers = getSFmembers(service,sfGroupId);
var code = updateMyContacts(service,sfGroupMembers);
console.log(email + ' ' + code);
}
}
function updateMyContacts(service,contacts){
var url = 'https://people.googleapis.com/v1/contactGroups/myContacts/members:modify'
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
payload: JSON.stringify({'resourceNamesToAdd': contacts}),
method: 'POST',
contentType:"application/json",
muteHttpExceptions:true
});
var respCode = response.getResponseCode()
return respCode
}
function getSFgroup(service){
//returns the resource name for the salesforce group or returns false
var url = 'https://people.googleapis.com/v1/contactGroups'
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
method: 'GET',
contentType:"application/json",
muteHttpExceptions:true
});
var respCode = response.getResponseCode()
if (respCode = 200){
var contactGroups = JSON.parse(response.getContentText()).contactGroups
for (var i in contactGroups){
if (contactGroups[i].name == 'Salesforce Sync'){
return contactGroups[i].resourceName
}
}
} else {
console.warn(respCode);
}
return false;
}
function getSFmembers(service, groupId){
//returns a list of groupMember names to add to myContacts
var url = 'https://people.googleapis.com/v1/'+groupId+'?maxMembers=10000';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
method: 'GET',
contentType:"application/json",
muteHttpExceptions:true
});
var respCode = response.getResponseCode()
if (respCode = 200){
var groupMembers = JSON.parse(response.getContentText()).memberResourceNames
return groupMembers;
} else {
console.warn(respCode);
}
}
function getService(USER_EMAIL) {
return OAuth2.createService('peopleAPI'+USER_EMAIL)
// Set the endpoint URL.
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
// Set the private key and issuer.
.setPrivateKey(PRIVATE_KEY)
.setIssuer(CLIENT_EMAIL)
// Set the name of the user to impersonate. This will only work for
// Google Apps for Work/EDU accounts whose admin has setup domain-wide
// delegation:
// https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
.setSubject(USER_EMAIL)
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getScriptProperties())
// Set the scope. This must match one of the scopes configured during the
// setup of domain-wide delegation.
.setScope('https://www.googleapis.com/auth/contacts');
}