Sending messages through FCM was working yesterday, but today I'm getting this error:
Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
As a first step, I created a new JSON file in Firebase, but I'm still getting the same error. How can I fix this?
public async Task SendPushNotificationAsync(string fcmToken, string title, string message)
{
if (string.IsNullOrWhiteSpace(fcmToken))
{
_logger.LogWarning("FCM token is null or empty");
throw new ArgumentException("FCM token cannot be null or empty", nameof(fcmToken));
}
if (FirebaseApp.DefaultInstance == null)
{
_logger.LogError("Firebase App is not initialized. DefaultInstance is null");
throw new ApplicationException("Firebase App is not initialized");
}
var notification = new Message
{
Token = fcmToken,
Notification = new FirebaseAdmin.Messaging.Notification
{
Title = title,
Body = message
},
Apns = new ApnsConfig()
{
Aps = new Aps()
{
Alert = new ApsAlert()
{
Title = title,
Body = message
},
Sound = "default",
Badge = 1,
},
}
};
try
{
_logger.LogInformation("Sending push notification to token: {Token}", fcmToken);
var messageId = await FirebaseMessaging.DefaultInstance.SendAsync(notification); // Error Line
_logger.LogInformation("Push notification sent successfully. Message ID: {MessageId}", messageId);
}
catch (Exception e)
{
_logger.LogError(e, "Error sending push notification to token {Token}", fcmToken);
throw new ApplicationException($"Error sending push notification: {e.Message}", e);
}
}
Service CTOR:
public FirebasePushService(IConfiguration configuration, ILogger<FirebasePushService> logger, IWebHostEnvironment environment)
{
_logger = logger;
// Initialize Firebase Admin SDK if not already initialized
if (FirebaseApp.DefaultInstance == null)
{
var firebaseConfigPath = configuration["Firebase:ConfigPath"];
if (string.IsNullOrEmpty(firebaseConfigPath))
{
throw new ApplicationException("Firebase:ConfigPath configuration is missing");
}
// Try to find the file in multiple locations, using ContentRootPath as primary source
var possiblePaths = new[]
{
firebaseConfigPath,
Path.Combine(environment.ContentRootPath, firebaseConfigPath),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, firebaseConfigPath),
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", firebaseConfigPath)
};
var configPath = possiblePaths.FirstOrDefault(File.Exists);
if (configPath == null)
{
_logger.LogError("Firebase configuration file not found in any of these locations: {Paths}",
string.Join(", ", possiblePaths));
throw new ApplicationException(
$"Firebase configuration file not found in any of these locations: {string.Join(", ", possiblePaths)}");
}
try
{
_logger.LogInformation("Initializing Firebase with config file: {ConfigPath}", configPath);
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile(configPath)
});
_logger.LogInformation("Firebase initialized successfully");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize Firebase Admin SDK");
throw;
}
}
}
fcmTokenvalue, and how you're callingSendPushNotificationAsync.