Reputation: 357
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),
Upvotes: 23
Views: 7941
Reputation: 11
I recently had this problem and the way I fixed it was to keep how the .json file was generated when I created the key
serviceAccount.json
{
"type": "",
"project_id": "",
"private_key_id": "",
"private_key": "",
"client_email": "",
"client_id": "",
"auth_uri": "",
"token_uri": "",
"auth_provider_x509_cert_url": "",
"client_x509_cert_url": "",
"universe_domain": ""
}
then reference it in a variable using require,
import * as admin from "firebase-admin/app";
var serviceAccount = require("./serviceaccount.json");
admin.initializeApp({
credential: admin.cert(serviceAccount),
});
or you could use import and have it like this, but then you would have to set resolveJsonModule to true in the tsconfig.json
import * as serviceAccount from "./serviceaccount.json";
admin.initializeApp({
credential: admin.cert(serviceAccount as admin.ServiceAccount)
});
Upvotes: 1
Reputation: 1
In my case I have needed add "assert { type: "json" }" to import the credentials with no errors. Like this:
import serviceAccount from "./service-account.json" assert { type: "json" };
const params = {
type: serviceAccount.type,
projectId: serviceAccount.project_id,
privateKeyId: serviceAccount.private_key_id,
privateKey: serviceAccount.private_key,
clientEmail: serviceAccount.client_email,
clientId: serviceAccount.client_id,
authUri: serviceAccount.auth_uri,
tokenUri: serviceAccount.token_uri,
authProviderX509CertUrl: serviceAccount.auth_provider_x509_cert_url,
clientC509CertUrl: serviceAccount.client_x509_cert_url
}
initializeApp({ credential: cert(params)});
Upvotes: 0
Reputation: 89
I have a very simple method, you just have give that import a type. In my case object.
import * as serviceAccount from './service-account.json';
const credentialObject:object = serviceAccount;
Upvotes: 0
Reputation: 41
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
});
Upvotes: 4
Reputation: 2162
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
});
Upvotes: 45
Reputation: 1104
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
});
Upvotes: 3