【问题标题】:Firebase Cloud Functions - Authentication error when testing functions with firebase functions:shellFirebase Cloud Functions - 使用 firebase 功能测试功能时出现身份验证错误:shell
【发布时间】:2019-08-18 15:43:48
【问题描述】:

我正在测试一些 Firebase Cloud 功能,当我使用 firebase functions:shell 在本地测试它们时,我收到此错误:

@firebase/database: FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to 
initializeApp() via the \"credential\" property failed to fetch a valid
 Google OAuth2 access token with the following error: \"Error fetching
access token: Error while making request: getaddrinfo ENOTFOUND 
metadata.google.internal metadata.google.internal:80. Error code: 
ENOTFOUND\"."}

当我尝试访问函数内部的admin.database() 时似乎会发生这种情况。

这些是我正在使用的 npm 模块:

"dependencies": {
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^3.1.0",
  },

这里有什么我遗漏的吗?我试过卸载firebase-tools 并通过npm i -g firebase-tools 重新安装,但没有帮助。

我也在通过admin.initializeApp() 初始化应用程序

【问题讨论】:

  • 您解决了吗?我也有同样的问题...谢谢!

标签: firebase google-cloud-functions firebase-tools


【解决方案1】:

我遇到了同样的问题。
如果您查看Admin Authentication API Errors,您会看到错误app/invalid-credential 是由于:

用于验证 Admin SDK 的凭据不能用于执行所需的操作。某些身份验证方法(例如 createCustomToken() 和 verifyIdToken() )需要使用证书凭据而不是刷新令牌或应用程序默认凭据来初始化 SDK。有关如何使用证书凭据对 Admin SDK 进行身份验证的文档,请参阅初始化 SDK。

您需要手动指定您的凭据。

验证服务帐号并授权其访问 Firebase 服务,必须生成 JSON 格式的私钥文件。

为您的服务帐户生成私钥文件:

  1. 在 Firebase 控制台中,打开设置 > 服务帐户。
  2. 点击 Generate New Private Key,然后点击 Generate Key 确认。
  3. 安全存储包含密钥的 JSON 文件。

我建议您按照Initialize the SDK 部分中描述的说明进行操作。
这为我解决了这个问题。

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 2020-10-25
    • 2018-07-19
    • 2020-03-03
    • 2022-06-16
    • 2017-06-16
    • 2017-11-06
    • 2021-05-24
    • 2017-07-30
    相关资源
    最近更新 更多