2

Currently trying to build my first android app with a GCM API key which will be used by a piece of marketing software to send push notifications.

Wanted to get some help validating my google cloud message (GCM) API key with a curl request. I've tried using a online curl builder but the results dont match the success or error message im expecting.

The below request has been copied from the push sdk documentation. If it comes back with a 401 error i need to update my GCM project to a FCM project.

curl --header "Authorization: key=AIzaSyBIuNzItgztXS31MYdl0xnszQcAUO7lbOg"
--header Content-Type:"application/json"
https://android.googleapis.com/gcm/send -d "{\"registration_ids\":[\"ABC\"]}"

I would obviously love to know how to do this myself also.

Thanks in advance.

AL.
  • 36,815
  • 10
  • 142
  • 281
chris
  • 861
  • 1
  • 7
  • 15
  • Hi Chris. Just to clarify, you were wondering how to send a simple request to verify the Server Key validity? – AL. May 23 '17 at 04:56
  • Hi AL. Yes thats correct. If you receive the following response, the API Key is valid. {"multicast_id":5464097959083008962,"success":0,"failure":1,"canonical_ids":0,"results": [{"error":"InvalidRegistration"}]} However, if you receive this response, the API key is invalid and you must go to step 4: Unauthorized

    Unauthorized

    Error 401

    – chris May 23 '17 at 05:20
  • Have you considered using [Postman](https://stackoverflow.com/documentation/firebase-cloud-messaging/8242/firebase-cloud-messaging/26577/sending-downstream-messages-using-postman#t=201705230456569285061) to try it? – AL. May 23 '17 at 06:03
  • Sorry for replying again AL. but it seems I'm doing something wrong as I'm getting a bad request - See: http://imgur.com/a/J3Mfl – chris May 23 '17 at 07:36
  • Hey @chris. No problem. The JSON in your request should be included in the *Body* tab, not in the URL itself. Then remove the `-d` part as well. Also, as best practice, always avoid disclosing your Server Key (the value used for Authorization) to the public. :) – AL. May 23 '17 at 07:42
  • @AL. thanks I'll give that a go tomorrow as im just about to shutdown – chris May 23 '17 at 07:56
  • No worries. I'll add in an answer with the steps and screenshots so you could check it later too. – AL. May 23 '17 at 07:58

1 Answers1

2

As per my comment, you could try sending a simple downstream message using Postman.

  1. Set request type to POST and provide the URL.

    enter image description here

  2. Set the headers:

    • Authorization = =<Server Key> (here, when you're just starting, it is suggested to proceed with using FCM instead of GCM, since a new valid Server Key can only be generated by creating a Firebase Project).
    • Content-Type = application/json

    enter image description here

  3. Set the (JSON payload) body (as raw):

    {
        "registration_ids" : ["ABC"]
    }
    

    enter image description here

  4. Click on Send .

You should receive an InvalidRegistration, a 401, or an Invalid Legacy Server Key... error:

  • InvalidRegistration error means the token(s) are invalid, but the Server Key is valid.
  • 401 means invalid credentials, for this particular request, it's the Server Key.
  • Invalid (legacy) Server-key delivered or Sender is not authorized to perform request. means you attempted to use an old format Server Key (like the one in your post). New Server Key's have more characters. Usual response is like this:

    <HTML>
    <HEAD>
        <TITLE>Invalid (legacy) Server-key delivered or Sender is not authorized to perform request.</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
        <H1>Invalid (legacy) Server-key delivered or Sender is not authorized to perform request.</H1>
        <H2>Error 401</H2>
    </BODY>
    </HTML>
    
Graham
  • 7,431
  • 18
  • 59
  • 84
AL.
  • 36,815
  • 10
  • 142
  • 281