2

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!

AL.
  • 36,815
  • 10
  • 142
  • 281
doe
  • 971
  • 2
  • 11
  • 27

1 Answers1

1

...do I need to manually generate the token each time?

Yes, because the token expires.

For how log the token would be valid?

From my experience, the (default) time may vary from 30mins to 1hr. Nonetheless, I would suggest not putting an exact timer on it. Also see OAuth2 and Google API: Access token expiration time?

What is the proper way of doing it in my case?

Well. The proper way is to set up a server dedicated for this task. However, if you are doing it manually, then what you did on your post is the simplest way (I think) that this would work.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • Is there any way you see to automate this process? Running the Android code to get token each time when you need to send an FCM message doesn't seem to be a feasible solution. Is there anyway to get it done through a script or something like that? – doe Oct 06 '19 at 17:47
  • @doe Maybe a python script (there is a python sample code for fetching the oauth token in the docs). With that said, I'm not that experienced with python scripting nor I have worked on a no-server solution with FCM to provide further advice. Cheers! – AL. Oct 06 '19 at 18:07