Reputation: 3
Im new to firebase and we're building a spring boot project using firebase admin sdk where we want to send notification to our mobile users.
I built a simple class that was able to send a notification to FMS successfully.
I downloaded Firebase Emulator and changed my code to use it. After realizing the emulator is not needed for us, I reverted my code and now I'm getting the following error:
Caused by: com.google.api.client.http.HttpResponseException: 401 Unauthorized
POST https://fcm.googleapis.com/v1/projects/workmerk-eec65/messages:send
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
Im so confused because, it works with the http methods using postman with the same firebase json file, but using admin sdk is throwing this error. So I think it's safe to say its not my firebase config especially because it was working before.
Also confused because it seems to only NOT work on my machine and IS working successfully on my colleague's machine even with mobile receiving it. Any help is appreciated thank you so much!
I believe my program is still trying to authenticate using emulator, but im not exactly sure... I also believe i removed all traces of the firebase emulator (deleted folder and checked for global env variables)
Here is my config class:
@Configuration
public class FirebaseConfig {
@Value("${firebase.config}")
private String firebaseConfigPath;
@Value("${firebase.useEmulator:false}")
private boolean useEmulator;
@PostConstruct
public void initialize() {
try {
InputStream serviceAccount = getClass().getClassLoader().getResourceAsStream(firebaseConfigPath);
if (serviceAccount == null) {
throw new RuntimeException("Firebase configuration file not found in classpath.");
}
FirebaseOptions.Builder optionsBuilder = FirebaseOptions.builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccount))
.setProjectId("workmerk-eec65");
// if (useEmulator) {
// // Emulator configuration
// FirestoreOptions firestoreOptions = FirestoreOptions.newBuilder()
// .setEmulatorHost("localhost:8085")
// .build();
// optionsBuilder.setFirestoreOptions(firestoreOptions)
// .setCredentials(new EmulatorCredentials());
// }
FirebaseOptions options = optionsBuilder.build();
if (FirebaseApp.getApps().isEmpty()) {
FirebaseApp.initializeApp(options);
}
} catch (Exception e) {
throw new RuntimeException("Failed to initialize Firebase", e);
}
}
}
Upvotes: 0
Views: 96
Reputation: 36
Try replacing
InputStream serviceAccount = getClass().getClassLoader().getResourceAsStream(firebaseConfigPath);
by
InputStream serviceAccount = IOUtils.toInputStream(firebaseConfigPath, StandardCharsets.UTF_8)
and pass directly your json file content
Also in my case the project id is already mentioned in the json file, if it's the same for you, you might want to delete this .setProjectId("workmerk-eec65")
Upvotes: 0