【问题标题】:How to connect to Firebase by Firebase Admin against a proxy?如何通过 Firebase 管理员针对代理连接到 Firebase?
【发布时间】:2017-01-18 06:44:26
【问题描述】:

目前,我正在使用 Firebase Admin SDK 在 NodeJS 服务器端应用程序中连接 Firebase 数据库。

但我没有找到通过代理设置连接 Firebase 的选项,或者它可以检测到我的系统 HTTP_PROXY 环境变量。

当我通过node index.js 运行节点脚本时,收到一些类似这样的超时消息(我知道在我的工作网络中,我无法直接连接到 Firebase)。

Error: Credential implementation provided to initializeApp() via the "credential
" property failed to fetch a valid Google OAuth2 access token with the following
 error: "connect ETIMEDOUT 216.58.200.237:443".                                 
    at ....erver\node_modules\firebase-adm
in\lib\firebase-app.js:74:23                                                    
    at process._tickCallback (internal/process/next_tick.js:103:7)                                                                                             

我还使用浏览器通过代理访问 Firebase 控制台,它可以工作。

但是如何在 NodeJS 服务器端脚本中解决这个问题?

【问题讨论】:

  • Admin Node.js SDK 似乎未正确初始化。你能分享你的初始化代码(admin.initializeApp())吗?您的代理设置可能会阻止 SDK 请求创建与 Firebase 服务通信所需的 Google OAuth2 访问令牌。该请求发送到https://www.accounts.google.com/o/oauth2/token 端口 443。我会检查以确保您的代理设置没有阻止这些请求。
  • 我已经在不同的环境中尝试了我的代码,如果网络可以连接到 Firebase/Google,它就可以工作。
  • 我在代理后面,无法初始化 firebase,有什么帮助吗?

标签: node.js firebase proxy firebase-realtime-database firebase-admin


【解决方案1】:

如果您运行 NodeJS 进程的主机上的日期和时间设置不正确,也会发生此错误。确保保持服务器时间同步。

完整的错误信息: Error: 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: invalid_grant (Invalid JWT: Token must be a short-lived token and in a reasonable timeframe)". The most likely cause of this error is using a certificate key file which has been revoked. Make sure the key ID for your key file is still present at https://console.firebase.google.com/iam-admin/serviceaccounts/project. If not, generatea new key file at https://console.firebase.google.com/project/_/settings/serviceaccounts/adminsdk.

【讨论】:

  • 对我来说,它实际上是说:“要解决 (1),请重新同步服务器上的时间。”
  • @electrobuddha 我的错误与 Electrobuddha 非常不同,显然我的错误有超时,因为我们无法连接到中国的任何 google 服务,我们必须使用一个代理来解决这个问题。 Electrobuddha 的回答投票率很高,但确实与我的问题相关,请删除。
  • @Garbit 仔细阅读我的问题和我的评论,这个答案与我的问题无关。
【解决方案2】:

昨天遇到了同样的问题,解决了。

让我们直截了当,您收到此错误是因为您所在地区禁止使用 Google 服务,因此您应该通过代理访问 Firebase。 Here's a blog explaining how it is done.

对于这种特定情况,您应该

  1. 准备一个允许您访问 Google 服务的代理服务器,然后
  2. 通过npm或yarn安装https-proxy-agent包,然后
  3. 像这样在您的 Firebase 应用程序初始化代码中包含代理
import HttpsProxyAgent from 'https-proxy-agent';
import * as admin from 'firebase-admin';
...
const agent = new HttpsProxyAgent('url to your proxy server');
admin.initializeApp({
  credential: admin.credential.applicationDefault(agent), 
  // Or any function you would like to use to provide your application's credentials
  // But remember to include the proxy agent in the parameter
  httpProxy: agent
});

但请记住不要将此更改提交到您的存储库中,因为此问题专门出现在无法访问 Google 服务的地区。

【讨论】:

    猜你喜欢
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 2017-07-03
    • 2017-09-23
    • 2019-03-14
    • 1970-01-01
    • 2020-12-14
    相关资源
    最近更新 更多