Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

SITUATION:

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

Everything works fine until I do npm start and go to:

http://localhost:8080

I am met with the following text on the blank page:

Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information.

Which makes no sense since I am using OAuth. I followed the link and read the page, but I have no GOOGLE-APPLICATION-CREDENTIALS field anywhere, and nothing about it in the tutorial.


QUESTION:

Could you please reproduce the steps and tell me if you get the same result ?

(takes 5 minutes)

If not, what could I have done wrong ?

like image 499
Coder1000 Avatar asked Feb 04 '17 17:02

Coder1000


12 Answers

Yes, I had the same error. It's annoying cause Google Cloud Platform docs for their "getting started" bookshelf tutorial does not mention this anywhere. Which means that any new developer who tries this tutorial will see this error.

Read this: https://developers.google.com/identity/protocols/application-default-credentials

I fixed this issue by running: gcloud auth application-default login

In order to run thisgcloud auth application-default login Visit: https://cloud.google.com/sdk/install 1) You have to install sdk into your computer 2) That will enable you to run the code 3) Log in to your associated gmail account then you are good to go!

This will make you login, and after that you code locally will use that authentication.

like image 159
newbreedofgeek Avatar answered Oct 03 '22 06:10

newbreedofgeek


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

like image 36
Caner Avatar answered Oct 03 '22 07:10

Caner


  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
like image 26
Bira Avatar answered Oct 03 '22 06:10

Bira


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!

like image 27
Blasanka Avatar answered Oct 03 '22 06:10

Blasanka


If you're running the app locally, then the gcloud beta auth application-default login command should suffice for acquiring local credentials (I updated the tutorial to say so).

When running the app on Google Compute Engine, if the Compute Engine instance was created with the proper scopes (cloud-platform should be sufficient) then the app will authenticate with Google Cloud Platform APIs automatically without any extra work on your part.

like image 25
jdobry Avatar answered Oct 03 '22 06:10

jdobry


I was facing the same issue. It got fixed with following command.

gcloud auth application-default login

It stores default gcloud cloud credentials on your system and uses the same.

like image 20
Tal Avatar answered Oct 03 '22 06:10

Tal


Go here: https://firebase.google.com/docs/admin/setup#initialize_the_sdk and follow the instructions to create a private key. Then after you have downloaded your private key open command prompt in the project directory and do the following command:

 set GOOGLE_APPLICATION_CREDENTIALS=C:\YOUR-PATH\YOUR-KEY.json
like image 26
HMaziad Avatar answered Oct 03 '22 08:10

HMaziad


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)
});
like image 24
Maitraiya Mali Avatar answered Oct 03 '22 06:10

Maitraiya Mali


You have to create an object of your SessionsClient. Here I will provide some steps, so you can run your code like a charm.

  1. You have to go into your Dialogflow dashboard.
  2. Click on setting ( Left navbar top-right gear icon)
  3. in the General tab click Service Account link ( it will redirect you to another screen)
  4. If you have a service account then ignore step 5
  5. Create a service account ( Top-center +icon button)
    • Now you have a service account on a list click on
  6. From the action, field presses the 3 vertical dots and create a key.
  7. Download the JSON file on your local computer.
  8. Assign object to your sessionClient.
const sessionClient = new dialogflow.SessionsClient({
 keyFilename: "/var/www/html/moqatrips/dialog-flow.json"
});
like image 41
Akshay Mistry Avatar answered Oct 03 '22 07:10

Akshay Mistry


For all people using firebase, what it worked for me was passing the credentials to the KeyManagementServiceClient constructor

const serviceAccount = require('../keys/file.json'); //<- your firebase credentials
const client = new KeyManagementServiceClient({
      credentials: serviceAccount,
    });
like image 2
pedrommuller Avatar answered Oct 03 '22 08:10

pedrommuller


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",
  },
like image 2
Lê Vũ Huy Avatar answered Oct 03 '22 06:10

Lê Vũ Huy


I also had this error problem, here I had not created the object of

keyFilename (stores the credentials of the api)

in the sessionClient object for nodejs app.

const sessionClient = new dialogflow.SessionsClient({
                keyFilename: "./keyCredentials.json"  
       });
        
           const sessionPath = sessionClient.sessionPath(projectId, sessionId);

To download 'keyCredentials.json' goto:

https://console.cloud.google.com/apis/credentials/serviceaccountkey

Also add the path of this file to the system variables

  1. In windows open powershell
  2. type GOOGLE_APPLICATION_CREDENTIALS = [PATH_TO_SERVICE_ACCOUNT_JSON_FILE]
like image 1
arg0100 Avatar answered Oct 03 '22 08:10

arg0100