Search code examples
firebasegoogle-sheetsgoogle-apps-script

Appscript cant connect to firebase via API


I tried to create a function to connect my Appscript to Firebase database. for some reason it shows me:


2:29:44 PM Error
TypeError: file.blob is not a function getServiceAccountKey @ firebase/firebase.gs:7


Here's my code:


const CREDENTIALS_FILE_ID = "Your File ID";
const PROJECT_ID = "Project ID";


function getServiceAccountKey() {
  var file = DriveApp.getFileById(CREDENTIALS_FILE_ID);
   JSON.parse(file.blob().getDataAsString());
}

function testFunc(){
  console.log(getServiceAccountKey());
}

function getAccessToken() {
  var serviceAccount = getServiceAccountKey();
  var tokenUrl = "https://www.googleapis.com/oauth2/v4/token";
  var payload = {
    grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
    assertion: createJWT(serviceAccount),
  };

  var options = {
    method: "post",
    payload: payload,
  };

  var response = UrlFetchApp.fetch(tokenUrl, options);
  var token = JSON.parse(response.getContentText()).access_token;

  return token;
}

I also tried to use other methods such as .stringify() and still wont work.


Solution

  • .getBlob() method

    Your code is correct and will work with native JavaScript. However, if you're using a different method, the syntax may vary.

    Script Used:

    function getServiceAccountKey() {
      var file = DriveApp.getFileById(CREDENTIALS_FILE_ID);
       JSON.parse(file.getBlob().getDataAsString());
    }
    

    Referrence:

    getBlob()