What is the `Authorization` part of the http post request of Google's Firebase Downstream message?

18,032

Solution 1

According to About Firebase Cloud Messaging Server documentation:

Authentication

To send a message, the app server issues a POST request. For example:

https://fcm.googleapis.com/fcm/send

A message request consists of two parts: the HTTP header and the HTTP body.

The HTTP header must contain the following headers:

  • Authorization: key=YOUR_SERVER_KEY

Make sure this is the server key, whose value is available in your Firebase project console under Project Settings > Cloud Messaging. Client keys or any other keys are rejected by FCM.

  • Content-Type: application/json for JSON; application/x-www-form-urlencoded;charset=UTF-8 for plain text.

     If Content-Type is omitted, the format is assumed to be plain text.
    

UPDATE (2016/11/16)

Perhaps you should consider using the FCM token instead of the server keys as the note inside the following screenshot

enter image description here


UPDATE (2016/12/08)

Firebase has upgraded the server keys to a new version and recommended that you upgrade to the newest version. Please see the screenshot below:

enter image description here

Solution 2

i have also faced this error. The issue is with the server key. Don't use the api key from google-services.json file.

You should use the server api key from your firebase console project, at the cloud messaging tab project keys: https://console.firebase.google.com/project/firebase-probiz/settings/cloudmessaging

Share:
18,032

Related videos on Youtube

roostaamir
Author by

roostaamir

Updated on June 25, 2022

Comments

  • roostaamir
    roostaamir almost 2 years

    I want to try to send a message using google's FCM messaging service and as the document says, the http request should be something like this:

    https://fcm.googleapis.com/fcm/send
    Content-Type:application/json
    Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
    
    { "data": {
        "score": "5x1",
        "time": "15:10"
      },
      "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
    }
    

    My problem is that I have not idea what Authorization's value should be and when I delete this header and make the request, I get error 401:Unauthorized.I think this must be some kind of api key or something but I cannot find it in my project. Can anyone help me?

    PS: for testing purposes I currently am using this site to send a messsage to my device

  • roostaamir
    roostaamir almost 8 years
    Thank you so much. The documentation is not well-organized in my opinion, but thank you again for your answer
  • FredLoh
    FredLoh almost 8 years
    Hey @BNK would you mind posting example curl or httpie code? I can't seem to get the Authorization to work. Thanks!
  • BNK
    BNK almost 8 years
    @FredLoh at the Google's link in my answer, at "Checking the validity of a server key", you will find curl example
  • Sagar Patil
    Sagar Patil over 7 years
    I am getting HTTP/1.1 400 Bad Request my post is fcm.googleapis.com/fcm/send Content-Type:application/json Authorization:key=GduFdg......wK8B4Uwk2xxx... { "data": { "message": "post message", "version" : "2.0: }, "to" : "fasdsadasdsadasj9N40:APA91b....5nX--HG.....7wyTCN" } what is wrong?
  • BNK
    BNK over 7 years
    @SagarPatil you got 400, not 401 as the OP, so fyi firebase.google.com/docs/cloud-messaging/…