Hantsy
Hantsy

Reputation: 9291

How to connect to Firebase by Firebase Admin against a proxy?

Currently, I am using Firebase Admin SDK to connect a Firebase database in a NodeJS server side application.

But I do not find an option to connect Firebase via proxy settings, or it can detect my system HTTP_PROXY environment variable.

When I run the node script by node index.js, and got some timeout messages like this(I know in my work network, I can not connect to Firebase directly).

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)                                                                                             

I also use browser to access the firebase console via proxy, it works.

But how to resolve this issue in NodeJS server side scripts?

Upvotes: 4

Views: 5905

Answers (2)

PotatoPie
PotatoPie

Reputation: 11

Faced with the exact same problem yesterday, got it solved.

Let's get it straight, you are getting this error because Google services are banned in your region, therefore you should access firebase through a proxy. Here's a blog explaining how it is done.

For this specific case, you should

  1. Prepare a proxy server that allows you to access Google services, then
  2. Install the https-proxy-agent package through npm or yarn, then
  3. Include the proxy in your firebase app initilization code like this
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
  httpAgent: agent
});

But remember not to commit this change into your repo, since this problem appears specifically in regions without access to Google services.

Upvotes: 1

ElectroBuddha
ElectroBuddha

Reputation: 648

This error also happens if the date and time on your host machine where you run NodeJS process is not set right. Make sure to keep the server time synced.

The full error message: 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.

Upvotes: 11

Related Questions