首页 > 解决方案 > 如何解决“错误:无法使用可用凭据初始化 Google Cloud Firestore 客户端。”?

问题描述

尝试使用我的 Firebase Cloud Functions 部署时,Firebase deploy出现以下错误:

Error: Error occurred while parsing your function triggers.

Error: Failed to initialize Google Cloud Firestore client with the available credentials. Must initialize the SDK with a certificate credential or application default credentials to use Cloud Firestore API.
    at FirebaseFirestoreError.FirebaseError [as constructor] (/functions/node_modules/firebase-admin/lib/utils/error.js:42:28)
    at new FirebaseFirestoreError (/functions/node_modules/firebase-admin/lib/utils/error.js:220:23)
    at getFirestoreOptions (/functions/node_modules/firebase-admin/lib/firestore/firestore.js:96:11)
    at initFirestore (/functions/node_modules/firebase-admin/lib/firestore/firestore.js:105:19)
    at new FirestoreService (/functions/node_modules/firebase-admin/lib/firestore/firestore.js:43:32)
    at /functions/node_modules/firebase-admin/lib/firebase-app.js:261:20
    at FirebaseApp.ensureService_ (/functions/node_modules/firebase-admin/lib/firebase-app.js:351:23)
    at FirebaseApp.firestore (/functions/node_modules/firebase-admin/lib/firebase-app.js:259:28)
    at FirebaseNamespace.fn (/functions/node_modules/firebase-admin/lib/firebase-namespace.js:327:45)
    at Object.<anonymous> (/functions/lib/users.js:6:27)

我怎样才能避免这种情况?

标签: firebasegoogle-cloud-firestore

解决方案


1.解决方案

更新

您可以通过执行简单地更新您的 Firebase 管理员npm i firebase-admin。感谢@samthecodingman 提供信息。

以前的

解决此问题的一种方法是通过执行npm i firebase-admin@8.9.0因为它似乎只影响版本来降级 Firebase 管理员的版本8.9.1


2.解决方案

解决问题的另一种方法是export GOOGLE_APPLICATION_CREDENTIALS="path/to/key.json"在命令行中运行。您可以按照以下步骤获取 Google 应用程序凭据:

  1. 打开 Google Cloud Console 的服务帐户窗格
  2. 确保选择了 App Engine 默认服务帐户,并使用右侧的选项菜单选择创建密钥。
  3. 出现提示时,选择 JSON 作为密钥类型,然后单击创建。

有关更多信息,请参阅 Firebase文档


推荐阅读