I have an old project where the back-end is completely handled by Firebase (I don't have a dedicated server). I used to use postman to send topic messages via FCM to the endpoint fcm.googleapis.com/fcm/send
with header Authorization : key=<myKey>
Now it gives me 403
error, when I checked the firebase documentation,I got to know that Apps using the FCM legacy HTTP API should consider migrating to the HTTP v1 API
I went through the documentation and it says the authorization key now look like
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
I was stuck at how to create this authorization key for my project, so that I can use it to trigger messages using postman or a curl request
I executed the following piece of code in my android project
private void getAccessToken() {
String[] SCOPES = {"https://www.googleapis.com/auth/firebase.messaging"};
GoogleCredential googleCredential = null;
googleCredential = GoogleCredential
.fromStream(streamFromAsset("service-account.json"))
.createScoped(Arrays.asList(SCOPES));
googleCredential.refreshToken();
return googleCredential.getAccessToken();
}
and this code gave me an access token similar to ya29.ElqKBGN2Ri_Uz...HnS_uNreA
Now I could send messages from postman using this token.
But still I'm confused, whatever I have done for getting the token is the proper way of doing it or not. The documentation says
Use your Firebase credentials together with the Google API Client Library for your preferred language to retrieve a short-lived OAuth 2.0 access token:
The token is a short lived one. If I had a server, the server code would automatically obtain the new token whenever token gets expired. Since I do not have a server, do I need to manually generate the token each time? For how log the token would be valid? What is the proper way of doing it in my case?
Thanks in advance!