Building an application on Google Apps Script and Firebase for backend integration. Problem – We cannot get admin access to our Realtime Database through our service account with the keys we generated.
We are using the key provided through the adminSDK, and verified that the service account has Realtime Database admin access
We are using the following oAuthScopes
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/script.external_request", "https://www.googleapis.com/auth/userinfo.email"
These are our security rules in the Realtime Database –
{ "rules":
{ ".read": "auth != null",
".write": "auth != null"
}
}
This is our class –
class FirebaseServiceAdminSDK {
constructor(config) {
this.firebaseUrl = config.databaseURL;
this.clientEmail = config.clientEmail;
this.privateKey = config.privateKey;
this.tokenUri = config.token_uri
}
getService() {
return OAuth2.createService('Firebase')
.setTokenUrl('https://oauth2.googleapis.com/token')
.setIssuer(this.clientEmail)
.setPrivateKey(this.privateKey)
.setTokenUrl(this.tokenUri)
}
checkAccess() {
const service = this.getService();
if (!service.hasAccess()) {
throw new Error('Firebase service does not have access. ' + service.getLastError());
}
}
fetchData(url, options) {
const response = UrlFetchApp.fetch(url, {options, muteHttpExceptions: true});
return JSON.parse(response.getContentText());
}
read(path) {
this.checkAccess();
const service = this.getService();
const url = `${this.firebaseUrl}/${path}.json`;
const options = {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
}
};
return this.fetchData(url, options);
}
}
New contributor
Kanator is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.