googleapis / google-api-nodejs-client

Google's officially supported Node.js client library for accessing Google APIs. Support for authorization and authentication with OAuth 2.0, API Keys and JWT (Service Tokens) is included.
https://googleapis.dev/nodejs/googleapis/latest/
Apache License 2.0
11.27k stars 1.91k forks source link

oauth2 client causes process to hang after initializing oauth client #3408

Closed zwhitchcox closed 6 months ago

zwhitchcox commented 6 months ago

So, I'm trying to run a test, and in the test I use this code:

export const oauth2Client: OAuth2Client = new google.auth.OAuth2(
    process.env.GOOGLE_CLIENT_ID,
    process.env.GOOGLE_CLIENT_SECRET,
    process.env.GOOGLE_REDIRECT_URI,
);
// Set the refresh token in the client
oauth2Client.setCredentials({
    refresh_token: process.env.GOOGLE_REFRESH_TOKEN,
});

export async function getAccessToken() {
    try {
        const { credentials } = await oauth2Client.refreshAccessToken(); // Refresh the access token
        return credentials.access_token;
    } catch (error) {
        console.error("Error refreshing access token", error);
        throw new Error("Could not refresh access token");
    }
}

The problem is that whenever I include the getAccessToken, the process will not close; it just hangs indefinitely.

So, I'm wondering how I can close the client after I have gotten the access token so that the process doesn't hang?

I did not see this anywhere in the docs, so I'm not sure if it's just me having this problem.

Even if I only request the token in non-testing mode:

import { getGoogleAuthHeaders as _getGoogleAuthHeaders } from "google";

export function getGoogleAuthHeaders() {
  if (process.env.NODE_ENV === "test") {
    return {};
  }
  return _getGoogleAuthHeaders();
}

I have the same problem. Prisma has a function $disconnect which will release any hanging connections. I'm wondering if there is something like this for the google client where I could remove all hanging connections?