-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathringscout.gs
More file actions
208 lines (174 loc) · 6.42 KB
/
Copy pathringscout.gs
File metadata and controls
208 lines (174 loc) · 6.42 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"use strict";
/******************************************************************************/
// Configuration
/******************************************************************************/
// Grab config variables from the Google Sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var USERNAME = sheet.getRange(21, 2).getValue();
var PASSWORD = sheet.getRange(22, 2).getValue();
var CLIENT_ID = sheet.getRange(23, 2).getValue();
var CLIENT_SECRET = sheet.getRange(24, 2).getValue();
// Add Custom Menu
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu("RingRx")
.addItem("Get Token", "getToken")
.addItem("Get Billing Info", "getBillingInfo")
.addItem("Get PBX Callbacks", "getPBXCallbacks")
.addToUi();
}
/******************************************************************************/
// Helper Functions
/******************************************************************************/
// Get multiple script properties in one call, then log them all.
function logUserProperties() {
var userProperties = PropertiesService.getUserProperties();
var data = userProperties.getProperties();
for (var key in data) {
Logger.log("Key: %s, Value: %s", key, data[key]);
}
}
// Communication Monitor
function logUrlFetch(url, opt_params) {
var params = opt_params || {};
params.muteHttpExceptions = true;
var request = UrlFetchApp.getRequest(url, params);
Logger.log("Request: >>> " + JSON.stringify(request));
var response = UrlFetchApp.fetch(url, params);
Logger.log("Response Code: <<< " + response.getResponseCode());
Logger.log("Response Text: <<< " + response.getContentText());
if (response.getResponseCode() >= 400) {
throw Error("Error in response: " + response);
}
return response;
}
/******************************************************************************/
// RingRx Calls
/******************************************************************************/
// Create a token from user credentials
function getToken() {
var service = getRingRxService_();
if (service.hasAccess()) {
Logger.log("App has access.");
var api =
"https://portal.ringrx.com/auth/token?username=USERNAME&password=PASSWORD";
var options = {
method: "POST",
muteHttpExceptions: true,
};
var response = logUrlFetch(api, options);
// Parse the JSON reply
var json = response.getContentText();
var data = JSON.parse(json);
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(2, 2).setValue(data["token_type"]);
sheet.getRange(3, 2).setValue(data["access_token"]);
sheet.getRange(4, 2).setValue(data["expires_in"]);
sheet.getRange(5, 2).setValue(data["refresh_token"]);
} else {
Logger.log("App has no access yet.");
// open this url to gain authorization from RingRx
var authorizationUrl = service.getAuthorizationUrl();
Logger.log(
"Open the following URL and re-run the script: %s",
authorizationUrl
);
}
}
// Get PBX Callbacks
function getPBXCallbacks() {
var service = getRingRxService_();
if (service.hasAccess()) {
Logger.log("App has access.");
var api = "https://portal.ringrx.com/pbxcallbacks";
var headers = {
Authorization: "Bearer " + sheet.getRange(3, 2).getValue(),
Accept: "application/json",
};
var options = {
headers: headers,
method: "GET",
muteHttpExceptions: true,
};
var response = logUrlFetch(api, options);
} else {
Logger.log("App has no access yet.");
// open this url to gain authorization from RingRx
var authorizationUrl = service.getAuthorizationUrl();
Logger.log(
"Open the following URL and re-run the script: %s",
authorizationUrl
);
}
}
// Get customer billing information for current account
function getBillingInfo() {
var service = getRingRxService_();
if (service.hasAccess()) {
Logger.log("App has access.");
var api = "https://portal.ringrx.com/billing";
var headers = {
Authorization: "Bearer " + sheet.getRange(3, 2).getValue(), //getRingRxService_().getAccessToken(),
Accept: "application/json",
};
var options = {
headers: headers,
method: "GET",
muteHttpExceptions: true,
};
var response = logUrlFetch(api, options);
} else {
Logger.log("App has no access yet.");
// open this url to gain authorization from RingRx
var authorizationUrl = service.getAuthorizationUrl();
Logger.log(
"Open the following URL and re-run the script: %s",
authorizationUrl
);
}
}
/******************************************************************************/
// Oauth2 Library Services
/******************************************************************************/
// Create RingRx service for persisting the authorized token
function getRingRxService_() {
return (
OAuth2.createService("RingRx")
// Set the endpoint URLs, which are the same for all Google services.
// .setAuthorizationBaseUrl('https://portal.ringrx.com/auth/token')
// .setTokenUrl('https://portal.ringrx.com/auth/token')
.setAuthorizationBaseUrl("https://accounts.google.com/o/oauth2/auth")
.setTokenUrl("https://accounts.google.com/o/oauth2/token")
// Set the client ID and secret, from the Google Developers Console.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set the name of the callback function to be invoked to complete
// the OAuth flow.
.setCallbackFunction("authCallback")
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
// Set the scopes to request (space-separated for Google services).
.setScope(
"https://www.googleapis.com/auth/script.external_request https://www.googleapis.com/auth/spreadsheets"
)
);
}
// Handle the callback
function authCallback(request) {
var ringrxService = getRingRxService_();
var isAuthorized = ringrxService.handleCallback(request);
if (isAuthorized) {
return HtmlService.createHtmlOutput("Success! You can close this tab.");
} else {
return HtmlService.createHtmlOutput("Denied. You can close this tab");
}
}
// Reset the authorization state, so that it can be re-tested.
function reset() {
getRingRxService_().reset();
}
// Logs the redict URI to register in the Google Developers Console.
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}