Could not load the default credentials? (Node.js Google Compute Engine tutorial)

后端 未结 14 1602
情话喂你
情话喂你 2020-12-04 19:20

SITUATION:

I follow this tutorial: https://cloud.google.com/nodejs/tutorials/bookshelf-on-compute-engine

Everything works fine until I do

14条回答
  •  清歌不尽
    2020-12-04 19:25

    I got this error because of initially I did like below:

    var admin = require("firebase-admin");
    admin.initializeApp(); // I didnt add anything because firebaserc file include appName
    

    It worked when I deployed the functions but not in serve. So this is how I solved it:

    • Go to the firebase Project settings(click on setting icon from side nav).
    • Click on the Service accounts.
    • Copy the admin sdk configuration snippet from selecting your pro. lang.

    Ex (node.js):

    var admin = require("firebase-admin");
    
    var serviceAccount = require("path/to/serviceAccountKey.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://your-domain.firebaseio.com"
    });
    
    • Now we need to add serviceAccountKey.json file.
    • Click on the Manage service account permissions in top right corner.
    • Now, you will see services accounts for your project, in the table find the row with column name name and value firebase-adminsdk, in that row click on Action dots and select Create key.
    • From the pop up dialog select Key type as json and press create button.
    • You will prompt to download the file, download it to your functions directory in project(You can customize this as you want and if you pushing to github, make sure to ignore that file).
    • Now, if you save it into the same directory where you are initializeApp(), access that file like: ./socialape-15456-68dfdc857c55.json(In my case, both files are located: functions/index.js and functions/services.son in functions directory and in index.js file, I initialed my firebase admin sdk).

    Ex(node.js):

    const functions = require('firebase-functions');
    
    var admin = require("firebase-admin");
    
    var serviceAccount = require("./myapp-15456-68dfdc857c55.json");
    
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://myapp-15456.firebaseio.com"
    });
    

    It's a best and good idea to create .env file and include your file there and access it as others mentioned. I leave that part to you.

    Hope this help someone on the planet. Regards!

提交回复
热议问题