0

The following error appears when you start the Admin SDK:

UnhandledPromiseRejectionWarning: Error: Failed to parse private key: Error: Too few bytes to read ASN.1 value

This is my code used to start the Admin SDK:

admin.initializeApp({
    credential: admin.credential.cert({
        projectId: 'Extracted from the firebase console',
        clientEmail: 'Project settings -> Service Account -> Firebase service account',
        privateKey: '-----BEGIN PRIVATE KEY-----\n<KEY>\n-----END PRIVATE KEY-----\n'
}),

KEY = Retrieved from the text of the private key file.Generated in Console

I also tried using the console snippet and gave this error:

SyntaxError: /data/data/com.termux/files/home/textos-da-ju/scripts/gerarPDF/serviceAccountKey.json: Unexpected token e in JSON at position 0

Does anyone know how to start this admin SDK correctly? Node version: 11.14.0

1 Answers1

0

See https://firebase.google.com/docs/admin/setup .

Note: The FIREBASE_CONFIG environment variable is included automatically in Cloud Functions for Firebase functions that were deployed via the Firebase CLI.

If you want to use Admin SDK in Cloud Functions for Firebase functions then only the following code

// Initialize the default app
var admin = require('firebase-admin');
var app = admin.initializeApp();

If you need to use serviceAccountKey.json then like the following code.


// import or require serviceAccountKey.json
// See https://stackoverflow.com/questions/7163061/is-there-a-require-for-json-in-node-js .
// Change path to your serviceAccountKey.json
import * as serviceAccountKey from "./serviceAccountKey.json";

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

admin.initializeApp({
    credential: admin.credential.cert({
        projectId: serviceAccountKey.project_id,
        clientEmail: serviceAccountKey.client_email,
        privateKey: serviceAccountKey.private_key
    }),
    databaseURL: "Your Database URL",
    projectId: "Your Project Id",
    storageBucket: "Your Storage Bucket"
});
zkohi
  • 2,486
  • 1
  • 10
  • 20
  • In my case I am trying with serviceAccountKey.json, but the way you portray it in your code it seems like this file is a JSON but when I open it with notepad I only see a lot of character and not a JSON with objects. – Guilherme Benevides May 25 '19 at 19:57
  • I discovered my problem was downloading the private key from the "wrong" place or the console of the firebase in the Brazilian language has a problem. In my case I had to download the key from the google cloud console. – Guilherme Benevides May 25 '19 at 20:05
  • Here's the right place to find your admin .json :
    Projet settings => account of service. At the bottom of the page, you can find out each admin sdk for different programming language.
    – DataHearth May 25 '19 at 20:24
  • you can change path to your serviceAccountKey.json in code and see Admin SDK Reference also. https://firebase.google.com/docs/reference/admin – zkohi May 25 '19 at 22:24