2

I'm calling Firebase from Google Apps Script.

I am able to connect to Firebase and read/write data using the Firebase Google Apps Script library here https://sites.google.com/site/scriptsexamples/new-connectors-to-google-services/firebase

However this library doesn't seem to have support for the createUser functions. How should I create a new user in Firebase using Apps Script with the function createUserWithEmailAndPassword(email, password)?

Here is what I'm trying to do in Google Apps Script -

function create_user(email, password) {
    var firebaseUrl = "https://myfb.firebaseio.com/";
    var secret = "mysecret";
    var base = FirebaseApp.getDatabaseByUrl(firebaseUrl, secret);

    base.auth().createUserWithEmailAndPassword(email, password)
      .catch(function(error) {
        var errorCode = error.code;
        var errorMessage = error.message;
        Logger.log(errorMessage);
    }); 
}

Any advice appreciated.

Regards Rahul

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
krahul
  • 51
  • 4

1 Answers1

2

You can just use the raw libraries instead of this wrapper script. Use firebase.js client library directly: https://www.gstatic.com/firebasejs/3.6/firebase.js

inject external javascript file in google app script(Google Docs) shows you how to import the external library script.

You can then run your client code:

var config = {
  apiKey: "AIzaS...",
  authDomain: "xxxxxxx.firebaseapp.com",
  databaseURL: "https://xxxxx.firebaseio.com"
}; 
firebase.initializeApp(config);
firebase.auth().createUserWithEmailAndPassword(email, password)...
Community
  • 1
  • 1
bojeil
  • 29,642
  • 4
  • 69
  • 76
  • Thanks Bojeil. I tried your suggestion of injecting external js via eval. That line does not throw any error - great. However, when I then try firebase.initiatlizeApp(config) it throws an error “firebase not defined”. How should I define/initialize firebase? If I try var ref = new Firebase(…), it gives me a timeout error on this line. Thanks in advance Rahul – krahul Jan 29 '17 at 09:19
  • With `eval(UrlFetchApp.fetch('https://www.gstatic.com/firebasejs/8.7.1/firebase-app.js').getContentText()); eval(UrlFetchApp.fetch('https://www.gstatic.com/firebasejs/8.7.1/firebase-auth.js').getContentText());`, I got error `Error: The XMLHttpRequest compatibility library was not found.` – BingLi224 Jul 15 '21 at 03:49