我使用firebase功能,我想将服务帐户密钥json初始化为凭证,我收到错误
类型' {"类型":字符串; " project_id":string; " private_key_id":string; " private_key":string; "客户_...'不能分配给' string |类型的参数ServiceAccount&#39 ;. 键入' {" type":string; " project_id":string; " private_key_id":string; " private_key":string; "客户_...'没有与' ServiceAccount'。
类型相同的属性
我的index.ts文件
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"
}
此代码行中的错误
credential: admin.credential.cert(serviceAccount),
答案 0 :(得分:2)
我也经历过。解决方案是将类型ServiceAccount
转换为该类型,然后将从该json文件导入的整个对象转换为该类型。
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
});
答案 1 :(得分:0)
以下代码对我有用:
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
});