获取访问令牌时出错:发出请求时出错:getaddrinfo ENOTFOUND metadata.google.internal。有时会发生错误?

Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal. ERROR HAPPENS SOMETIMES?

前言,这是错误。

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 错误。

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

之前,我有:

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 环境中是完全自动化的,无需提供环境变量或其他配置,因此强烈建议应用程序使用这种初始化 SDK 的方式运行 在 Compute Engine、Kubernetes Engine、App Engine 和 Cloud Functions 上。"

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

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