forked from brucemcpherson/testGoaNoCreds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastoresa.gs
76 lines (56 loc) · 2.34 KB
/
datastoresa.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
// Data store using service account
/**
* this is how to do a webapp which needs authentication
* @param {*} e - parameters passed to doGet
* @return {HtmlOurput} for rendering
*/
function doGetDataStoreSA (e) {
// this is pattern for a WebApp.
// passing the doGet parameters (or anything else)
// will ensure they are preservered during the multiple oauth2 processes
// change this to whatever store & credentials name are being used
var goa = cGoa.GoaApp.createGoa('DriverDatastore_serviceaccount',PropertiesService.getScriptProperties()).execute(e);
if (goa.needsConsent()) {
// this should never happen with a service account
throw 'you should never need consent with a service account';
}
// if we get here its time for your webapp to run and we should have a token, or thrown an error somewhere
if (!goa.hasToken()) throw 'something went wrong with goa - did you check if consent was needed?';
// This is a webapp doing whaever its supposed to do
// getParams is used to retrieve the original parameters passed to this function
var result = testDataStore (goa.getToken(), goa.getParams() );
// now return it as normal
return HtmlService.createHtmlOutput (result.getContentText())
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function dataStoreSA (params) {
// pick up the token refreshing if necessary
var goa = cGoa.GoaApp.createGoa('DriverDatastore_serviceaccount', PropertiesService.getScriptProperties()).execute(params);
if (!goa.hasToken()) {
throw 'no token retrieved';
}
// do a test - passing the token and any parameters that arrived to this function
Logger.log (testDataStore (goa.getToken(), goa.getParams(params) ));
}
/**
* this is your main processing - will be called with your access token
* @param {string} accessToken - the accessToken
* @param {*} params any params
*/
function testDataStore (accessToken,params) {
var options = {
method: "POST",
contentType : "application/json" ,
muteHttpExceptions : true,
headers: {
"authorization": "Bearer " + accessToken,
},
payload:JSON.stringify({
"query": {
"kinds": [{"name":"polymerdbab"}]
}
})
};
return UrlFetchApp.fetch(
"https://www.googleapis.com/datastore/v1beta2/datasets/xliberationdatastore/runQuery", options);
}