API Key for Gcm Is Suddenly Invalid? Unauthorized (401) Error

GCM (Google Cloud Messaging) returns suddenly 401

Not sure why sending push notifications using the Android Key stopped working, since it used to work before. I managed to send a Push Notification to an Android app by using the Server key instead of the Android key like this:

curl --header "Authorization: key=<my_server_key>" --header Content-Type:"application/json" https://android.googleapis.com/gcm/send -d "{\"to\":\"/topics/my_topic\", \"data\":{\"category\":\"help\", \"title\":\"Test\", \"text\":\"Test\", \"param\":\"\"}}"

Apparently this seems to be the correct method now for Android:
Why Android key (api key) is invalid or that key is deprecated by GCM server?

I found a site which says that "Google made a change that stopped Android Keys from working for GCM/FCM for security reasons"
https://groups.google.com/forum/#!msg/firebase-talk/4lCPr-UIuY0/ShWZk6z2AAAJ

So the answer seems to be to use the "Server Key" to send Android GCM messages (not the "Android Key").

Unauthorized error 401 GCM error

Update: There is now a visible note in the GCM docs saying:

Starting from Sept. 2016 new server key can only be created in the Firebase Console using the Cloud Messaging tab of the Settings panel. Existing projects that need to create a new server key can be imported in the Firebase console without affecting their existing configuration.


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.

I've been seeing a lot of this concern this past few days. It would seem that unrestricted keys return 401 errors and it may be because they are enforcing the security for all API keys (where you see the warning).

As such, what I commonly recommend is to make use of the IP address restriction (seeing as this is for GCM, and it should be a Server Key) and add your server IP Address. See my answer here.

Google GCM server returns Unauthorized Error 401

Did you whitelist the IP of your server? This is not necessary by default for the browser key, but it is for the server key.

You can check it here:

https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access

GCM 401 Unauthorized - Where to get the right Server Key?

TL;DR:

To create a sender-id and server-key for GCM or FCM you can use the Firebase Console.

New projects should be created directly in Firebase Console, while existing Cloud Console projects can be imported using Import Google Project from the main page: http://console.firebase.google.com

Once you have a firebase project you can find the GCM / FCM credentials in

Project Setting > Cloud Messaging

More informations:

  1. After importing a Google Project into Firebase the same project will be accessible from both the Cloud Console and the Firebase Console. What changes is that you also have access to the Firebase API.

  2. While we strongly recommended to update the client SDK to Firebase Cloud Messaging, the credentials listed under Project Settings > Cloud Messaging can be used in GCM too.

Google GCM server returns Unauthorized Error 401 while subscribing

Finally it worked for me, two things i did it to work.

  1. Took the GCM.jar file from the sample project and placed it in my project . removed the other gcm dependencies

  2. Created a project in FCM Console (https://console.firebase.google.com/)
    and took the google-services.json and placed it in my android studio project under app. Took the web API key from FCM Console and placed it in the mobilefirst app project.(There are two keys now one for app and other for web).

I had to create a new project in the fcm console and new keys..



Related Topics



Leave a reply



Submit