Skip to content

Commit 65ff55d

Browse files
author
Eric Koleda
committed
Add samples for Firebase database
1 parent 341b7d3 commit 65ff55d

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

samples/FirebaseDB.gs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* This sample uses the Firebase Admin SDK to read from a Firebase Realtime
3+
* Database.
4+
* https://firebase.google.com/docs/reference/rest/database/
5+
* https://firebase.google.com/docs/admin/setup
6+
*/
7+
8+
var PROJECT_ID = '...';
9+
var PRIVATE_KEY = '...';
10+
var CLIENT_EMAIL = '...';
11+
12+
/**
13+
* Authorizes and makes a request to a Firebase Database.
14+
*/
15+
function run() {
16+
var service = getService();
17+
if (service.hasAccess()) {
18+
var url = 'https://' + PROJECT_ID + '.firebaseio.com/.json?shallow=true';
19+
var response = UrlFetchApp.fetch(url, {
20+
headers: {
21+
Authorization: 'Bearer ' + service.getAccessToken()
22+
}
23+
});
24+
var result = JSON.parse(response.getContentText());
25+
Logger.log(JSON.stringify(result, null, 2));
26+
} else {
27+
Logger.log(service.getLastError());
28+
}
29+
}
30+
31+
/**
32+
* Reset the authorization state, so that it can be re-tested.
33+
*/
34+
function reset() {
35+
var service = getService();
36+
service.reset();
37+
}
38+
39+
/**
40+
* Configures the service.
41+
*/
42+
function getService() {
43+
return OAuth2.createService('FirebaseDB')
44+
// Set the endpoint URL.
45+
.setTokenUrl('https://accounts.google.com/o/oauth2/token')
46+
47+
// Set the private key and issuer.
48+
.setPrivateKey(PRIVATE_KEY)
49+
.setIssuer(CLIENT_EMAIL)
50+
51+
// Set the property store where authorized tokens should be persisted.
52+
.setPropertyStore(PropertiesService.getScriptProperties())
53+
54+
// Set the scope and additional Google-specific parameters.
55+
.setScope([
56+
'https://www.googleapis.com/auth/userinfo.email',
57+
'https://www.googleapis.com/auth/firebase.database'
58+
]);
59+
}

0 commit comments

Comments
 (0)