web-auth-library icon indicating copy to clipboard operation
web-auth-library copied to clipboard

getAccessToken not working on Cloudflare Workers

Open oriollpz opened this issue 10 months ago • 9 comments

The function

const accessToken = await getAccessToken({
      credentials: credentials,
      scope: "https://www.googleapis.com/auth/cloud-platform",
      waitUntil: c.executionCtx.waitUntil,
});

Isn't working for me, it works locally, but when I use it in the worker it says this: TypeError: Illegal invocation

Anyone faced the same problem?

oriollpz avatar Apr 22 '24 15:04 oriollpz

I have the same problem, did you solve it ?

zewelor avatar May 22 '24 07:05 zewelor

@zewelor I stopped using this library and I used this instead.

oriollpz avatar May 22 '24 08:05 oriollpz

@zewelor I stopped using this library and I used this instead.

Hmm i see link to this ( web-auth-library ) library

zewelor avatar May 22 '24 11:05 zewelor

Same issue here.

Manouchehri avatar Jul 01 '24 20:07 Manouchehri

Use waitUntil: ctx.waitUntil.bind(ctx) instead.

Manouchehri avatar Jul 01 '24 21:07 Manouchehri

Use waitUntil: ctx.waitUntil.bind(ctx) instead.

but I got '{}' from my firestore.

RickYangzz avatar Jul 18 '24 04:07 RickYangzz

image Thanks everybody below this problem. I have been successful. This is my code.

RickYangzz avatar Jul 18 '24 09:07 RickYangzz

I tried it today and I'm getting following info:

TypeError: Illegal invocation: function called with incorrect `this` reference. See https://developers.cloudflare.com/workers/observability/errors/#illegal-invocation-errors for details.
A ReadableStream branch was created but never consumed. Such branches can be created, for instance, by calling the tee() method on a ReadableStream, or by calling the clone() method on a Request or Response object. If a branch is created but never consumed, it can force the runtime to buffer the entire body of the stream in memory, which may cause the Worker to exceed its memory limit and be terminated. To avoid this, ensure that all branches created are consumed.

 * Unused stream created:
    at null.<anonymous> (file:///C:/projects/parcels-api/node_modules/web-auth-library/dist/google/accessToken.js:105:36)
    at async getAccessToken (file:///C:/projects/parcels-api/node_modules/web-auth-library/dist/google/accessToken.js:127:12)
    at async getFirebaseToken (file:///C:/projects/parcels-api/src/utils/firebase-auth.ts:21:23)
    at async getAuthHeaders (file:///C:/projects/parcels-api/src/clients/firestore-client.ts:8:17)
    at async getUserAvailableCoverage (file:///C:/projects/parcels-api/src/clients/firestore-client.ts:17:19)
    at null.<anonymous> (async file:///C:/projects/parcels-api/.wrangler/tmp/dev-RVTltV/index.js:39419:38)
    at async dispatch (file:///C:/projects/parcels-api/node_modules/hono/dist/compose.js:29:17)
    at async cors2 (file:///C:/projects/parcels-api/node_modules/hono/dist/middleware/cors/index.js:65:5)
    at async dispatch (file:///C:/projects/parcels-api/node_modules/hono/dist/compose.js:29:17)
    at async poweredBy2 (file:///C:/projects/parcels-api/node_modules/hono/dist/middleware/powered-by/index.js:4:5)

arekgotfryd avatar Sep 07 '24 06:09 arekgotfryd

@arekgotfryd I had the same issue when using CF Workers without hono and executionCtx.

Here's how I ended up solving...

return getAccessToken({
    credentials: env.GOOGLE_CLOUD_CREDENTIALS,
    scope: 'https://www.googleapis.com/auth/cloud-translation',
    waitUntil: ctx.waitUntil.bind(ctx),
    env,
  });

Note how the waitUntil function is re-bound to (CF Worker native) ctx.

auniverseaway avatar Sep 18 '24 16:09 auniverseaway