firebase / firebase-admin-node

Firebase Admin Node.js SDK
https://firebase.google.com/docs/admin/setup
Apache License 2.0
1.63k stars 371 forks source link

Error when subscribing to topic: "An error occurred when trying to authenticate to the FCM servers" #2720

Closed Yash-Makwana1 closed 1 month ago

Yash-Makwana1 commented 1 month ago

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

I am attempting to subscribe a list of FCM tokens to a topic, but I encounter the following error message:

This occurs even though the necessary permissions are configured in the Firebase project.

messaging/authentication-error\",\"message\":\"An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenticate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions.

import * as admin from 'firebase-admin';
import { cert } from 'firebase-admin/app';

// Load service account credentials
const serviceAccount = require('./serviceAccount.json');

// Initialize Firebase Admin SDK
admin.initializeApp({
  credential: cert(serviceAccount),
});

export const firebaseSubscribeTopic = async (FCMTokens: string[], topic: string): Promise<void> => {
  try {
    const response = await admin.messaging().subscribeToTopic(FCMTokens, topic);
    if (response.errors.length > 0) {
      throw new Error(`${response.failureCount} of ${response.successCount + response.failureCount} 
          failed due to: ${JSON.stringify(response.errors)}`);
    } else {
      logger.info(`Successfully subscribed to topic: ${JSON.stringify(response)}`);
    }
  } catch (error: any) {
    logger.error(`Error subscribing to topic: ${JSON.stringify(error.message)}`);
  }
};

Additional Notes: I have checked the service account's permissions and ensured it has the necessary roles to access Cloud Messaging services. Please advise if there's anything I'm missing or if there's a bug with the SDK.

google-oss-bot commented 1 month ago

I found a few problems with this issue:

lahirumaramba commented 1 month ago

Do you have both Cloud Messaging and Firebase Cloud Messaging APIs enabled? See: https://stackoverflow.com/a/72659398/1526201

In the future please use StackOverflow for usage questions.

Yash-Makwana1 commented 1 month ago

@lahirumaramba Firebase cloud messaging is already enabled no service "cloud messaging" found.

lahirumaramba commented 1 month ago

It looks like a permission issue in your project and I don't think this is a bug in the SDK. Could you try with a different Firebase project and see if you can reproduce the issue?

If that is the case, to better investigate this issue, you can file a ticket to Firebase Support https://firebase.google.com/support where you can share more information for us for debugging.