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

后端 未结 14 1528
情话喂你
情话喂你 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!

    0 讨论(0)
  • 2020-12-04 19:25

    Another solution i found: in your package.json add an export command like this:

    "scripts": {
        "start": "export GOOGLE_APPLICATION_CREDENTIALS='./gcloud.json' && node ./bin/www --exec babel-node --presets babel-preset-env",
      },
    
    0 讨论(0)
  • 2020-12-04 19:25

    I provide another way which is set the credentials manually. For local development and running on GCP, like Cloud Function, Compute Engine.

    You can use a service account and grant this service account proper permission. e.g. Cloud Trace Admin

    Set private_key, client_email and projectId options for any GCP client library. These options you can get from the service account json file.

    For example, I am using Cloud Trace Node.js client library in my cloud functions.

    Before I set the credentials and projectId, I got this error:

    Error: Unexpected error while acquiring application default credentials: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information. at GoogleAuth. (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:248:31) at step (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:47:23) at Object.next (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:28:53) at fulfilled (/srv/node_modules/google-auth-library/build/src/auth/googleauth.js:19:58) at at process._tickDomainCallback (internal/process/next_tick.js:229:7)

    You can pass the credentials and projectId as environment variables. After setting credentials and projectId, the error is gone.

    const tracer = require('@google-cloud/trace-agent').start({
        samplingRate: 0,
        bufferSize: 1,
        credentials: {
          client_email: process.env.CLIENT_EMAIL,
          private_key: process.env.TRACE_AGENT_ADMIN,
        },
        projectId: process.env.X_GOOGLE_GCLOUD_PROJECT || process.env.PROJECT_ID,
      });
    

    X_GOOGLE_GCLOUD_PROJECT is a built-in environment variable for cloud function runtime

    0 讨论(0)
  • 2020-12-04 19:27
    1. Create a service account key using and download the json file. https://console.cloud.google.com/apis/credentials/serviceaccountkey

    2. Add this to your ENV file

      GOOGLE_APPLICATION_CREDENTIALS = "<PATH_TO_SERVICE_ACCOUNT_JSON_FILE>"

    E.g:

    GOOGLE_APPLICATION_CREDENTIALS=/Users/hello/Documents/ssh/my-10ebbbc8b3df.json
    
    0 讨论(0)
  • 2020-12-04 19:29

    use this to solve your issue. this actually works:- just put credential parameter and give reference to your key to it.

    const serviceAccount = require('../key.json');
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount)
    });
    
    0 讨论(0)
  • 2020-12-04 19:30

    There are 2 solutions for this problem. One option, as mentioned by others, is to use gcloud auth application-default login

    Second option is to set the environment variable GOOGLE_APPLICATION_CREDENTIALS. It should point to a file that defines the credentials. To get this file you need to follow these steps:

    Go to the API Console Credentials page.

    From the project drop-down, select your project.

    On the Credentials page, select the Create credentials drop-down, then select Service account key.

    From the Service account drop-down, select an existing service account or create a new one.

    For Key type, select the JSON key option, then select Create. The file automatically downloads to your computer.

    Put the *.json file you just downloaded in a directory of your choosing.

    This directory must be private (you can't let anyone get access to this), but accessible to your web server code.

    Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file downloaded.

    See https://developers.google.com/identity/protocols/application-default-credentials for details

    0 讨论(0)
提交回复
热议问题