Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FIREBASE WARNING: Provided authentication credentials are invalid

I am trying to use Firebase in node.js but every time I restart the server I am getting following error:

FIREBASE WARNING: Provided authentication credentials are invalid. This usually indicates your FirebaseApp instance was not initialized correctly. Make sure your apiKey and databaseURL match the values provided for your app at https://console.firebase.google.com/, or if you're using a service account, make sure it's authorized to access the specified databaseURL and is from the correct project.

Following is my index.js:_

var express = require('express');
var router = express.Router();
var mongoose=require('mongoose');
var admin=mongoose.model('admin');


var firebase = require("firebase");

// Initialize the app with no authentication
firebase.initializeApp({
	  serviceAccount: {
    projectId: "...",
    clientEmail: "...",
    privateKey: "-----BEGIN PRIVATE KEY-----...",
  },

      databaseURL: "..."
});

console.log("sfsaf")

// The app only has access to public data as defined in the Security Rules
			var db = firebase.database();
			var ref = db.ref("unitalk-b9145");
			var messagesRef = ref.child("messages");
			
			messagesRef.push({
				name:"Rupali",
				post:"Demo test of firebase"
			});

Although I have checked the path of service-account and databaseURl.. Please help..

like image 343
Rupali Avatar asked Nov 09 '22 06:11

Rupali


1 Answers

You can not log in with the service account using the "firabase" package. You need to use the "firabase-admin" package for this. You can find detailed information here (https://firebase.google.com/docs/database/admin/start).

UPDATED: 8 Nov 2016

go to : https://console.firebase.google.com

To use the Firebase Admin SDKs, you'll need a Firebase project, a service account to communicate with the Firebase service, and a configuration file with your service account's credentials.

  1. Navigate to the Service Accounts tab in your project's settings page.
  2. Select your Firebase project. If you don't already have one, click the Create New Project button. If you already have an existing Google project associated with your app, click Import Google Project instead.
  3. Click the Generate New Private Key button at the bottom of the Firebase Admin SDK section of the Service Accounts tab.
  4. After you click the button, a JSON file containing your service account's credentials will be downloaded. You'll need this to initialize the SDK in the next step.

Sample code;

var admin = require("firebase-admin");

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});
like image 112
Halil SAFAK Avatar answered Nov 14 '22 21:11

Halil SAFAK