googleapis / google-auth-library-nodejs

🔑 Google Auth Library for Node.js
Apache License 2.0
1.73k stars 381 forks source link

ECONNRESET #1695

Closed Jared00m closed 11 months ago

Jared00m commented 11 months ago

I have been working on a Flutter project that uses Firebase Functions. In the past, I was able to create a Firebase Function that retrieves a JWT access token with no problem. However, recently, I get the following error every time:

{"severity":"ERROR","message":"Unhandled error GaxiosError: Invalid response body while trying to fetch https://www.googleapis.com/oauth2/v4/token: read ECONNRESET\n

Here is my code:

import { JWT } from "google-auth-library";
import fs from "fs";
import * as functions from "firebase-functions";
const logger = functions.logger;

export const getJSONWebToken = functions.https.onCall(async (data, context) => {
  try {
    const keyFile = "./src/secrets/***************************";

    const keys = JSON.parse(fs.readFileSync(keyFile, "utf8"));

    const client = new JWT({
      email: keys.client_email,
      key: keys.private_key,
      scopes: ["https://www.googleapis.com/auth/cloud-platform"],
    });

    const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
    await client.request({ url });

    const res = await client.getAccessToken();

    if (res.token) {
      return { token: res.token };
    } else {
      console.log("no token retrieved");
      return { token: "no token " };
    }
  } catch (error) {
    logger.error(error);
    throw error;
  }
});

Any help would be greatly appreciated. I'm not a professional coder and I'm new to Firebase and Flutter.

danielbankhead commented 11 months ago

@Jared00m which version of Node are you running?

Jared00m commented 11 months ago

@danielbankhead , thanks for asking. I've tried it on Node 18, 16, 15, 14, and 12.

On node 15 I get a useful error that says:


data: {
      error: {
        code: 403,
        message: 'Forbidden',
        errors: [
          {
            message: 'Forbidden',
            domain: 'global',
            reason: 'forbidden'
          }
        ]
      }
    },

On Node 18 I get this error:


config: {
    method: 'POST',
    url: 'https://www.googleapis.com/oauth2/v4/token',
    data: {
      grant_type: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
      assertion: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.'
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'User-Agent': 'google-api-nodejs-client/9.2.0',
      'x-goog-api-client': 'gl-node/18.3.0',
      Accept: 'application/json'
    },
    responseType: 'json',
    paramsSerializer: [Function: paramsSerializer],
    body: '<<REDACTED> - See `errorRedactor` option in `gaxios` for configuration>.',
    validateStatus: [Function: validateStatus],
    errorRedactor: [Function: defaultErrorRedactor]
  },
  response: undefined,
  error: FetchError: Invalid response body while trying to fetch https://www.googleapis.com/oauth2/v4/token: read ECONNRESET
      at Gunzip.<anonymous> (/Users/jared/Coding/language-app-test/language-app/functions/node_modules/node-fetch/lib/index.js:400:12)
      at Gunzip.emit (node:events:539:35)
      at emitErrorNT (node:internal/streams/destroy:151:8)
      at emitErrorCloseNT (node:internal/streams/destroy:116:3)
      at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
    type: 'system',
    errno: 'ECONNRESET',
    code: 'ECONNRESET'
  },
  code: 'ECONNRESET'
}
danielbankhead commented 11 months ago

Investigating. Potentially related items:

danielbankhead commented 11 months ago

After discussing with the team, this is best handled by setting up automatic retries in this library.

I'll prioritize this work and it can be tracked here:

danielbankhead commented 9 months ago

This PR should provide some relief for the immediate issue: