10

Below is the exception I am getting even though my API key for server and browser application are valid. I checked it using curl. I send GCM request in both format UTF-8 and JSON. Testing it from outside corporate network.

java.io.IOException: Server returned HTTP response code: 401 for URL: https://android.googleapis.com/gcm/send
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1345)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1339)
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:993)
        at java.net.URLConnection.getContent(URLConnection.java:688)
        at sun.net.www.protocol.https.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:406)
        at gcm1.MessageUtil.sendMessage(MessageUtil.java:58)

May I know the reason and solution for this ?

Luke Baughan
  • 4,658
  • 3
  • 31
  • 54
chako
  • 161
  • 1
  • 2
  • 9
  • For others who are browsing just now, if you've suddenly started experiencing this error without making any specific changes (the Server Key) still the same, you probably need to import your project to Firebase. See my answer [here](http://stackoverflow.com/a/37801206/4625829). – AL. Oct 25 '16 at 08:21

3 Answers3

7

Here are the possible causes for 401 error when using GCM:

The sender account that you're trying to use to send a message couldn't be authenticated. Possible causes are:

  • Authorization header missing or with invalid syntax.
  • Invalid project ID sent as key.
  • Key valid but with GCM service disabled.
  • Request originated from a server not whitelisted in the Server Key IPs.

Check that the token you're sending inside the Authorization header is the correct API key associated with your project.

Source: https://developers.google.com/cloud-messaging/http-server-ref#error-codes

Laurent Meyer
  • 2,766
  • 3
  • 33
  • 57
azgolfer
  • 15,087
  • 4
  • 49
  • 46
  • I checked my API key using curl and its working fine. It returns response with success:1 in JSON format. Server is also white-listed in Server Key IPs. – chako Aug 13 '12 at 04:27
  • 2
    The thing that got me is that Google requires that you prefix your API key with 'key=' in the 'Authorization' header. This makes no sense to me (it's already in a header field; having 'key=' is superfluous), but once I added that in, problem solved. – aroth Sep 09 '13 at 04:35
  • Updated link: https://developers.google.com/cloud-messaging/http-server-ref#error-codes – Jason Feb 12 '16 at 23:28
0

In my case the problem was that I forgot to redeploy after setting correct API key in the datastore.

David Vávra
  • 18,446
  • 7
  • 48
  • 56
0

Please follow as par below instruction it's may help you.

Update: It would also seem that Migrating from GCM to FCM fixes the issue for 401 Unauthorized Error.

If you are just starting to use GCM, instead of creating a project in the Google Developers Console, do it in the Firebase Console. After creating the project, simply use the auto-generated Server Key. Here are the steps where to find the Server Key:

  1. Go to your Firebase Console and click on CREATE NEW PROJECT.
  2. Fill in your desired Project Name and select your Country. After this, the new Project should be active.
  3. Then on left-side panel, click on the gear button and select Project Settings.
  4. Then go to the Cloud-Messaging Tab.

For old GCM projects, you can simply Import the project to the Firebase Console:

  1. Go to your Firebase Console and click on IMPORT PROJECT.
  2. Select the project you want to import and your country.
  3. Click on ADD FIREBASE. After this, the new Project should be active.
  4. Then on left-side panel, click on the gear button and select Project Settings.
  5. Then go to the Cloud-Messaging Tab.
Nilesh Patel
  • 557
  • 5
  • 8