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
Google GCM server returns Unauthorized Error 401 while subscribing
Finally it worked for me, two things i did it to work.
Took the GCM.jar file from the sample project and placed it in my project . removed the other gcm dependencies
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..
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:
- Go to your Firebase Console and click on CREATE NEW PROJECT.
- Fill in your desired Project Name and select your Country. After this, the new Project should be active.
- Then on left-side panel, click on the gear button and select Project Settings.
- Then go to the Cloud-Messaging Tab.
For old GCM projects, you can simply Import the project to the Firebase Console:
- Go to your Firebase Console and click on IMPORT PROJECT.
- Select the project you want to import and your country.
- Click on ADD FIREBASE. After this, the new Project should be active.
- Then on left-side panel, click on the gear button and select Project Settings.
- 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.
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").
Related Topics
Remove Duplicate from String in PHP
Php: Check If a File Is Loaded Directly Instead of Including
PHP - Include a PHP File and Also Send Query Parameters
How to Echo a MySQLi Prepared Statement
Laravel 5.4 on PHP 7.0: Pdo Exception - Could Not Find Driver (Mysql)
Soap PHP Fault Parsing Wsdl: Failed to Load External Entity
Convert Jpg Image to Gif, Png & Bmp Format Using PHP
How to Check If a Url Exists in PHP
Convert Utc Offset to Timezone or Date
Ajax Passing Data to PHP Script
Pdo: MySQL Server Has Gone Away
Laravel 5.2 $Errors Not Appearing in Blade
Connecting to Remote MySQL Server Using PHP
Recursively Remove Empty Elements and Subarrays from a Multi-Dimensional Array