Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase import service throws error

I'm using firebase functions and I want to initializeApp with service account key json into credential and I get the error

Argument of type '{ "type": string; "project_id": string; "private_key_id": string; "private_key": string; "client_...' is not assignable to parameter of type 'string | ServiceAccount'. Type '{ "type": string; "project_id": string; "private_key_id": string; "private_key": string; "client_...' has no properties in common with type 'ServiceAccount'.

my index.ts file

 import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import {serviceAccount} from './serviceAccount'

console.log(functions.config())
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL:functions.config().firebase
});

export const firestore = admin.firestore();
export const firebase = admin.database();

serviceAccount.ts

export const serviceAccount = {

    "type": "service_account",
    "project_id": "lxxxxxx",
    "private_key_id": "xxxxxx",
    "private_key": "-----BEGIN PRIVATE KEY-----xxxxxxx---END PRIVATE KEY-----\n",
    "client_email": "firebase-axxxxx-9b58b.iaxxxceaccount.com",
    "client_id": "xxxxx",
    "auth_uri": "https://accounts.google.com/o/xxxxx",
    "token_uri": "https://accounts.google.com/o/oxxxxn",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "clixxxxxx": "https://www.googleapis.com/robot/v1/metadataxxxxirebase-adminsdk-uxxxxxxrviceaccount.com"


}

the error in this line of code

        credential: admin.credential.cert(serviceAccount),
like image 800
Manspof Avatar asked May 10 '18 11:05

Manspof


3 Answers

I've experienced this too. The solution is to bring the type ServiceAccount and cast to that type the whole object imported from that json file.

import firebase from 'firebase' 
import * as firebaseAdmin from 'firebase-admin' 
import firebaseConfig from '../firebaseConfig.json' 
import firebaseAccountCredentials from '../serviceAccountCredentials.json'

const serviceAccount = firebaseAccountCredentials as admin.ServiceAccount

firebaseAdmin.initializeApp({
    credential: firebaseAdmin.credential.cert(serviceAccount),
    databaseURL: firebaseConfig.databaseURL 
});
like image 94
Sebastian Sastre Avatar answered Oct 30 '22 20:10

Sebastian Sastre


I'm not very experienced with TypeScript, but I think this will work as a temporary solution until someone proposes a better one.

let regularObj = {};
Object.assign(regularObj, serviceAccount);

admin.initializeApp({
  credential: admin.credential.cert(regularObj),
  databaseURL: functions.config().firebase
});
like image 3
Dennis Persson Avatar answered Oct 30 '22 22:10

Dennis Persson


I've had an issue like this.
Try converting to JSON and back.
Like this:

admin.initializeApp({
  credential: admin.credential.cert(JSON.parse(JSON.stringify(serviceAccount))),
  databaseURL: functions.config().firebase
});
like image 1
Lawrecks Avatar answered Oct 30 '22 22:10

Lawrecks