-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAdminOauth2.gs
49 lines (42 loc) · 1.81 KB
/
AdminOauth2.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
function getAdminService() {
return OAuth2.createService('AdminEmail')
.setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
.setClientId(PropertiesService.getScriptProperties().getProperty("clientId"))
.setClientSecret(PropertiesService.getScriptProperties().getProperty("clientSecret"))
.setProjectKey('...')
.setCallbackFunction('authCallback')
.setPropertyStore(PropertiesService.getUserProperties())
.setScope('https://apps-apis.google.com/a/feeds/emailsettings/2.0/')
.setParam('login_hint', Session.getActiveUser().getEmail())
.setParam('access_type', 'offline')
.setParam('approval_prompt', 'force');
}
function showAuthWindow() {
var adminService = getAdminService();
if (!adminService.hasAccess()) {
var authorizationUrl = adminService.getAuthorizationUrl();
var template = HtmlService.createTemplate(
'<a href="<?= authorizationUrl ?>" target="_blank">Authorize</a> ' +
' access to your domains email settings. <br>NOTE: This script must be ran under a domain admins account with the proper privileges');
template.authorizationUrl = authorizationUrl;
var page = template.evaluate();
return page;
} else {
return HtmlService.createHtmlOutput("You have already authorized this service");
}
}
function authCallback(request) {
var adminService = getAdminService();
var isAuthorized = adminService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput('Success! You can close this tab.');
} else {
return HtmlService.createHtmlOutput('Denied. You can close this tab');
}
}
function clearService(){
OAuth2.createService('AdminEmail')
.setPropertyStore(PropertiesService.getUserProperties())
.reset();
}