【问题标题】:Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. ERROR HAPPENS SOMETIMES?获取访问令牌时出错:发出请求时出错:getaddrinfo ENOTFOUND metadata.google.internal。有时会发生错误?
【发布时间】:2022-01-18 07:05:50
【问题描述】:

首先,这是错误。

Promise { <pending> }
nodejsproject\node_modules\firebase-admin\lib\utils\error.js:44
        var _this = _super.call(this, errorInfo.message) || this;
                           ^
FirebaseAppError: 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. Error code: ENOTFOUND".
    
  errorInfo: {
    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. Error code: ENOTFOUND".'
  },
  codePrefix: 'app'
}

据我了解,此错误是因为我的参数无效或导致 firebase 初始化不正确。

现在我正在像这样初始化应用程序:initializeApp();

所以初始化工作。当我运行诸如const userInfo = await getAuth().verifyIdToken(authorization); 之类的命令时,该命令有效并且我得到了userInfo,但是当我运行诸如const userInfo = await getAuth().getUser(uuid); 之类的命令时它失败了。

据我所知,语法是正确的,我使用这些方法的方式也是正确的。我只是不确定为什么在使用一种方法而不是另一种方法时出现 initializeApp 错误。

【问题讨论】:

    标签: javascript typescript firebase firebase-authentication


    【解决方案1】:

    我想出了如何在不引发错误的情况下让这两个函数工作。为了解决我的问题,我更改了我的环境变量。

    以前,我有:

    GCLOUD_PROJECT="project-name"
    FIREBASE_CONFIG={keyDetailsHere}
    

    之后,我将它的值更改为服务帐户密钥的路径:

    GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
    

    我不确定为什么原来的那个在某些功能上工作(特别是getAuth().verifyIdToken())但在其他功能上却失败了。据我了解,initializeApp(),如果留空,将检查环境变量GOOGLE_APPLICATION_CREDENTIALS。如果找到它,它会使用您明确设置的凭据,如果没有,它会使用 Google 应用程序默认凭据。使用 Google 应用程序默认凭据设置后,它会查看 FIREBASE_CONFIG 环境变量以获取初始化选项。

    文档摘录:“由于默认凭据查找在 Google 环境中是完全自动化的,无需提供环境变量或其他配置,因此强烈建议在 Compute Engine、Kubernetes Engine 上运行的应用程序使用这种初始化 SDK 的方式、App Engine 和 Cloud Functions。”

    我的理解是我的原始设置不起作用,因为我是在本地开发而不是在 Google 环境中开发,因此它无法获取正确的 Google 应用程序默认凭据。

    作为参考,这里是文档:LINK

    【讨论】:

      猜你喜欢
      • 2018-04-02
      • 2021-05-10
      • 1970-01-01
      • 2021-04-22
      • 2019-01-06
      • 2020-07-08
      • 2017-03-05
      • 1970-01-01
      • 2012-07-14
      相关资源
      最近更新 更多