Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getAccessToken not working on Cloudflare Workers #28

Open
oriollpz opened this issue Apr 22, 2024 · 10 comments
Open

getAccessToken not working on Cloudflare Workers #28

oriollpz opened this issue Apr 22, 2024 · 10 comments

Comments

@oriollpz
Copy link

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?

@zewelor
Copy link

zewelor commented May 22, 2024

I have the same problem, did you solve it ?

@oriollpz
Copy link
Author

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

@zewelor
Copy link

zewelor commented May 22, 2024

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

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

@Manouchehri
Copy link

Same issue here.

@Manouchehri
Copy link

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

@RickYangzz
Copy link

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

but I got '{}' from my firestore.

@RickYangzz
Copy link

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

@arekgotfryd
Copy link

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)

@auniverseaway
Copy link

@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.

@niklasfjeldberg
Copy link

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

This worked for me, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants