GCM http 401 authorization error

77,905

Solution 1

Do they mean "api_key" (AIzaSy…) or the Project number like 8305134….?

It means Server key which should added in your curl script. The project number should not be used for sending message on server side.

To create the new server key, follow the steps,

  • Go to https://cloud.google.com/console/project
  • Click your Project
  • Go to APIs & auth on Left Panel
  • Select Credentials in submenu on Left Panel
  • Under Public API access, click on "Create New Key"
  • Choose 'Server'
  • On next dialog, Specify the whitelisted IPs, if you have any. Leaving it blank, the requests will be from any IP
  • Click Create.
  • You will find the new server key is created under "Public API Access"

This key is to be used for sending message in your script on server side.

How do I whitelist my Server Key IP?

You can whitelist while creating server key.

If I'm running the curl command, do I have to whitelist the IP-Address of my computer?

For testing purpose, you dont have to specify the IP. For commercial purpose, you may want to.

Warning setting IP to 0.0.0.0/0 as mentioned in the Getting Started guide will allow all IPv4 address, but not IPv6.

Solution 2

UPDATE:

Google has changed the console, now you have to:

  • Go to https://console.developers.google.com
  • Select your project (dropdown at top)
  • Select Overview in menu on Left Panel
  • Select Google Cloud Messaging under Mobile APIs title
  • Click Enable button
  • Then you have to create the credentials

DEPRECATED

I had the same problem ... then I realized that I had to add the Google Cloud Messaging for Android too.

  • Go to https://cloud.google.com/console/project
  • Click your Project
  • Go to APIs & auth on Left Panel
  • Select APIs in submenu on Left Panel
  • Search Google Cloud Messaging for Android (or/and Chrome) and put it ON

Hope it helps you!

Solution 3

I solved this error by using the Server Key instead of the API Key. You can get the Server Key from Firebase Console by clicking on the gear and clicking on Project Settings, after that go to the tab that says Cloud Messaging and get the server key, both the normal server key and the legacy server key will work.

This works on both Firebase and GCM so we do not need to declare two keys separately.

Solution 4

Finally found My problem

I made HTTP Authorization Header like this Authorization:[Api key]

Solution

I was missing 'key='+[Your Api key] in the Authorization header,

so the full HTTP header will be

Authorization:key=Apikey

Solution 5

I had the same problem. I was using the Android Key as mentioned in the instructions at http://developer.android.com/google/gcm/gs.html But I actually had to use the Server Key to get authenticated from my Fiddler window and PushSharp example.

Share:
77,905
A.G.
Author by

A.G.

Updated on October 17, 2020

Comments

  • A.G.
    A.G. over 3 years

    When my backend server sends post requests to GCM servers I get an authorization error HTTP 401.

    I followed the steps described here:

    http://developer.android.com/google/gcm/http.html#auth_error

    >> api_key=AIzaSyDEy3...
    
    >> curl --header "Authorization: key=$api_key" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send  -d "{\"registration_id\":\"ABC\"}"
    

    I get this:

    <HTML>
    <HEAD>
    <TITLE>Unauthorized</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
    <H1>Unauthorized</H1>
    <H2>Error 401</H2>
    </BODY>
    </HTML>
    

    In the troubleshooting it says:

    Authentication Error
    The sender account that you're trying to use to send a message couldn't be authenticated. Possible causes are:
        1. Authorization header missing or with invalid syntax.
        2. Invalid project number sent as key.
        3. Key valid but with GCM service disabled.
        4. Request originated from a server not whitelisted in the Server Key IPs.
    

    I have questions about that:

    1. Is the header in the curl request correct?
    2. Do they mean "api_key" (AIzaSy…) or the Project number like 8305134….?
    3. How do I whitelist my Server Key IP?
    4. If I'm running the curl command, do I have to whitelist the IP-Address of my computer?
  • Henrique de Sousa
    Henrique de Sousa about 10 years
    Been trying to deploy and configure GCM, your answer was the only one that helped me. Basically I've been trying to test using the curl commands but I was always spitting out "Unauthorized". That page was the first to give me a valid response, thank you very much :)
  • Nezneika
    Nezneika about 10 years
    Absolutely right, it must be Server Key for me too
  • majakthecoder
    majakthecoder about 9 years
    It works for me. I had the same problem with Parse.com and it's Push Notifications mechanism.
  • user590849
    user590849 about 9 years
    For me clearing the list of ip's worked. I did not have to re-generate my server key.0.0.0.0/0 does not work. I don't know why they have mentioned to add it in the documentation? How will GCM know that the request is coming from my desktop and not any actual server.
  • Julio Rodrigues
    Julio Rodrigues almost 9 years
    I also had to activate both Chrome and Android Cloud Messaging API's.
  • Mahendran Sakkarai
    Mahendran Sakkarai almost 9 years
    +1, Thanks @vijay-c Initialy I used the Android Application API Key in the server. So i got 401 unauthorized error. From your answer only I come to know we need to generate and use the Server Application API Key.
  • Quan Nguyen
    Quan Nguyen almost 8 years
    Yeah it works. So strange that my Android Key still worked in the last couple of months until today.
  • user1324887
    user1324887 almost 8 years
    @QuanNguyen, My android key had worked for me in the past too.
  • user1324887
    user1324887 almost 8 years
    @Vijay Vepakomma, Can you tell m whats the difference in Android vs Server key ?
  • Vijay Vepakomma
    Vijay Vepakomma over 7 years
    @user1324887 Android key is used by the app to send its notification to the OS, server key is for authenticating it to the push notification server
  • Wanna Coffee
    Wanna Coffee over 7 years
    Can you update with latest screens of console.cloud.google.com/iam-admin/projects? Seems it is been modified and i couldn't find the specified menu's.!