Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get and set data to Firebase with Node.js?

I'm learning the Firebase and Node.js. I want to get my Tweets from Twitter and send them to Firebase. I can retrieve my tweets from Twitter, but couldn't send these tweets to Firebase.

How can I send these tweets retrieved from Twitter to Firebase?

I've tried the following code:

var firebase = require('firebase');

// Initialize
var app = firebase.initializeApp({
    ServiceAccount: {
       projectId: "******",
       clientEmail: "****@gmail.com",
       privateKey: "-----BEGIN PRIVATE KEY-----\nkey\n-----END PRIVATE KEY-----\n"
    },
   databaseURL: "****.firebaseio.com"
}); 

// Set Sample Data
firebase.database().ref('/').set({
    username: "test",
    email: "[email protected]"
});

And I got the following error on console:

 Debugger listening on port 5858
 crypto.js:279
  var ret = this._handle.sign(toBuf(key), null, passphrase);
                         ^

Error: error:0906D064:PEM routines:PEM_read_bio:bad base64 decode
    at Error (native)
    at Sign.sign (crypto.js:279:26)
    at Object.sign (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\node_modules\jsonwebtoken\node_modules\jws\node_modules\jwa\index.js:54:45)
    at Object.jwsSign [as sign] (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\node_modules\jsonwebtoken\node_modules\jws\lib\sign-stream.js:23:24)
    at Object.JWT.sign (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\node_modules\jsonwebtoken\index.js:137:16)
    at authJwt (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\auth-node\auth.js:83:16)
    at fetchAccessToken (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\auth-node\auth.js:96:17)
    at app_.INTERNAL.getToken (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\auth-node\auth.js:196:14)
    at Zb.getToken (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\database-node.js:28:3496)
    at yh (C:\Users\admin\Desktop\myProject\myProject\node_modules\firebase\database-node.js:195:334)
 Press any key to continue...

Also, this my Firebase rules, I'm using these rules just for test purpose:

{
   "rules":
   {  
      ".read": true,
      ".write": true
   }
}
like image 655
Firat Eski Avatar asked May 22 '16 18:05

Firat Eski


2 Answers

I solved my problem. I just downloaded serviceAccount.json file from Firebase Console and inserted it into my project. The 'Service Account' file contains everything that I need.

var firebase = require('firebase');

firebase.initializeApp({
    databaseURL: 'https://*****.firebaseio.com',
    credential: 'myapp-13ad200fc320.json', // This is the serviceAccount.json file
});

Then the code below worked nicely.

firebase.database().ref('/').set({
    username: "test",
    email: "[email protected]"
});
like image 130
Firat Eski Avatar answered Sep 22 '22 03:09

Firat Eski


This specific syntax/library for service accounts in node apps is being deprecated. The new method of reaching firebase on a server (that is, not a consumer app, like IoT or desktop) is the firebase admin sdk.

Your initialization code should now go:

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"
});

You can still manually key in your credentials, but they're now assigned to a diff't property:

admin.initializeApp({
  credential: admin.credential.cert({
    projectId: "<PROJECT_ID>",
    clientEmail: "foo@<PROJECT_ID>.iam.gserviceaccount.com",
    privateKey: "-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n"
  }),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});
like image 37
Brandon Avatar answered Sep 20 '22 03:09

Brandon