firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.83k stars 890 forks source link

Request is missing required authention credential.Expected OAuth 2 access token cookie or other valid authentication credential. #8513

Open amitromi9470 opened 2 weeks ago

amitromi9470 commented 2 weeks ago

Operating System

Ubuntu 22.04.4

Environment (if applicable)

Chrome Version 126.0.6478.182, react: 17.0.1

Firebase SDK Version

firebase: 9.9.4

Firebase SDK Product(s)

Auth

Project Tooling

React, Javascript.

Detailed Problem Description

I am trying to register FCM token for 200 users,But out of 200 users for some users its failing to register token with 401 error.This is the error i am getting { "error":{ "code":401, "message":"Request is missing required authention credential.Expected OAuth 2 access token cookie or other valid authentication credential. see https://developers.google.com/identity/sign-in/web/devconsole project.", "status": UNAUTHORISED } }

Steps and code to reproduce issue

const firebaseApp = initializeApp(firebaseConfig);

export const messaging = getMessaging(firebaseApp); let swRegistration;

const requestForToken = async (registerFCMToken) => { try { swRegistration = await navigator.serviceWorker.register(${process.env.PUBLIC_URL}/firebase-messaging-sw.js); messaging.swRegistration = swRegistration;

const currentToken = await getToken(messaging, {
  serviceWorkerRegistration: swRegistration,
  vapidKey: 'BJAig9HybaAM035dc-e4TQp0Aa0B4TEWMSiBLy14qddxnNlxRNhaaVMXIlDTI11Q-Mil_O4He4GT6hCco2im4uU'
});
return currentToken;

} catch (err) { console.log('An error occurred while retrieving token. ', err); throw err; } };

google-oss-bot commented 2 weeks ago

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

amitromi9470 commented 2 weeks ago

fcMimage

amitromi9470 commented 2 weeks ago

I saw same issue has been raised 3 years back but didn't see any solution for that, can we get more clarity on this issue as in the comment of this issue https://github.com/firebase/firebase-js-sdk/issues/5081 its mentioned that its a backend issue and resolved without doing any changes in js sdk.

hsubox76 commented 2 weeks ago

I see "invalid argument" in your screenshotted error message. In your original post I see "Request is missing required authentication credential.". Are these happening at the same time or are you getting two different errors? Is this a web app or a Node app? What is the value of process.env.PUBLIC_URL? I thought web apps don't have access to process.env.

amitromi9470 commented 2 weeks ago

@hsubox76 its a react web app,The screenshot error message i am getting in browser console and whatever in the original post i am getting that in network logs.

hsubox76 commented 2 weeks ago

How do you have access to process.env if it's a web app? What do you mean by "network logs"? Are these two errors happening at the same time?

Is this still a problem if you use the latest version of Firebase? I see the version you're using is 2 years old.

amitromi9470 commented 1 week ago

@hsubox76 i don't have access to process.env instead i am making use of window object. Network logs means when you inspect the element the browser console will open, in that network tab is there. And yes both the issue happening at the same time with latesst firebase version as well. Screenshot from 2024-09-30 16-49-59

dlarocque commented 1 week ago

@hsubox76 i don't have access to process.env instead i am making use of window object. Network logs means when you inspect the element the browser console will open, in that network tab is there. And yes both the issue happening at the same time with latesst firebase version as well. Screenshot from 2024-09-30 16-49-59

If you are in a browser environment where the built-in process.env is undefined, what does process.env.PUBLIC_URL refer to in your code snippet?

The screenshot of the network request errors you shared shows a 400 bad request error mentioning that the request contains an invalid argument. Is the issue you are reporting caused by a 401 Unauthorized of a 400 bad request?

dlarocque commented 1 week ago

I saw same issue has been raised 3 years back but didn't see any solution for that, can we get more clarity on this issue as in the comment of this issue https://github.com/firebase/firebase-js-sdk/issues/5081 its mentioned that its a backend issue and resolved without doing any changes in js sdk.

Could you elaborate on what you mean here? My understanding is that there's another issue with FCM that was resolved three years ago- how does this relate to this issue?

amitromi9470 commented 1 week ago

@hsubox76 @dlarocque sorry my bad in the below code snippet swRegistration = await navigator.serviceWorker.register(${process.env.PUBLIC_URL}/firebase-messaging-sw.js); process.env.PUBLIC_URL is nothing but the base url(https://XXXXXXXXXXXXXXXXXXXXXX/console) of my website.

amitromi9470 commented 1 week ago

@hsubox76 @dlarocque Actually i am running selenium for doing FCM registration for 200 users and its failing(giving above error) only for 2-3 users.

dlarocque commented 1 week ago

@amitromi9470 Could you please respond to my second question here: https://github.com/firebase/firebase-js-sdk/issues/8513#issuecomment-2383460285

amitromi9470 commented 1 week ago

@dlarocque Please refer this comment https://github.com/firebase/firebase-js-sdk/issues/8513#issuecomment-2384795542 and for this The screenshot of the network request errors you shared shows a 400 bad request error mentioning that the request contains an invalid argument. Is the issue you are reporting caused by a 401 Unauthorized of a 400 bad request?:- I am not sure about this. Also my doubt is out of 200 users why it is showing invalid argument for only 2 - 3 users?

dlarocque commented 6 days ago

@dlarocque Please refer this comment #8513 (comment) and for this The screenshot of the network request errors you shared shows a 400 bad request error mentioning that the request contains an invalid argument. Is the issue you are reporting caused by a 401 Unauthorized of a 400 bad request?:- I am not sure about this. Also my doubt is out of 200 users why it is showing invalid argument for only 2 - 3 users?

I am not sure why it would fail for ~1% of registrations, assuming that they are all done under the same conditions.

For us to be able to determine whether this is a bug with the Firebase JS SDK, we need to be able to reproduce this issue. The snippet you have shared works reliably for me. Would you be able to share exact steps we can follow to reproduce this issue?

If you're not able to share steps for a reproduction, I recommend checking out Firebase Support.