0

I want to add HTTP V1 Access Token to my firebase cloud messaging service. I'm using python fore retrieving the access token.

import firebase_admin
from firebase_admin import credentials

cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)

I'm using this code. It's working fine but the expiry date for the access token is only one day. What is the way to increase the expiry date?

I want to get this bearer token. In the authorisation section.

POST https://fcm.googleapis.com/v1/projects/myproject-b5ae1/messages:send HTTP/1.1

Content-Type: application/json
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA

{
   "message":{
      "token":"token_1",
      "data":{},
      "notification":{
        "title":"FCM Message",
        "body":"This is an FCM notification message!",
      }
   }
}
Ayush Rawat
  • 152
  • 7
  • I don't see any code that generates tokens here. Can you clarify what returns the token valid of 1 day? – Dharmaraj Jan 31 '23 at 18:08
  • Firebase auth tokens last one hour and then are refreshed, see this for reference: https://stackoverflow.com/a/64991314/4044241 If this escenario is something different, add more information. – Gerardo Jan 31 '23 at 22:00

1 Answers1

0

To get the "access token" that is needed in the "Authorization" header, you will need to authenticate using the OAuth 2.0 process. This document explains how to get it with python.

Since you are already using the admin SDK, it will do the authentication for you. And, instead of making the HTTP request yourself, you can call the SDK methods for sending messages. See this example:

import firebase_admin
from firebase_admin import credentials

cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)

message = messaging.Message{
      "token" = "token_1",
      data={
            'score': '850',
            'time': '2:45',
        }
   }
response = messaging.send(message)
print('Successfully sent message:', response)

Here you can see an example with more options and details.

Gerardo
  • 3,460
  • 1
  • 16
  • 18