30

I created an Android API key for GCM Push Notification for my Android application.From the last two days, GCM server returns Unauthorized (401) response in PHP.

Is there any reason for Android API key invalid/expire? Or Is Android API key deprecated by GCM?

Still I'm not getting proper explanation regarding Why Android API key is invalid? Most of them prefer Server key instead of Android API key for GCM but not mentioning as to why not use Android API key.

Why do not use Android API Key in GCM? Is there any limitation for Android API key? Or Is GCM server blocks the notification/GCM server is making Android API key invalid if notification message exceeds per day limit?

AL.
  • 36,815
  • 10
  • 142
  • 281
kumar
  • 469
  • 2
  • 5
  • 10
  • did you update the necessary credentials in the PHP script? Did you include your package name in the google developer console? – Swaminathan V Jun 13 '16 at 12:14
  • @kumar I updated my answer. Check it out. If you are looking for an official reason, nothing seems to be disclosed yet. – AL. Jun 15 '16 at 03:15
  • Hi @kumar, I'm also encountering a similar issue with my app today. It has been working fine since 2 years, but suddenly since Friday, I've started receiving the same 401 unauthorized error. Have tried regenerating the server key as well, but didn't work out. Did you manage to solve it ? – Shubhral Aug 16 '16 at 09:43
  • You just delete that server key and create new server key. Now try it – kumar Aug 26 '16 at 05:05

1 Answers1

65

Update: The term being used is back to Server Key.

Just recently, there is now a Firebase Cloud Messaging Token (FCM Token) that is being rolled out. This is visible in the Cloud Messaging tab in your Firebase Console.


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.

For some reason, only a Server Key works now with GCM. The Android Key isn't the only one that seems to be invalid, all other Client API keys (Browser, iOS, Android) are.


When pertaining to the Server Key's description:

Create and use a Server key if your application runs on a server.

It is logical to use a Server key since you are using the API key on your Server, and it is working with GCM Connection server.

If you compare the FCM docs with the GCM docs (under Credentials), you can see that in GCM, it only states API key, while in FCM, it already specifies a Server key. Well, FCM is the newer version of GCM, may be it has something to do with it? I'm not so sure, but I think you get the gist of what I'm saying.

Update: In the GCM docs, Server Key is now indicated.


By default, after going through the Configure your API Project steps, a Server API key is generated (I tested it by selecting Android app). Same goes with . Whenever I create a new project, it just generates a Server Key automatically.

Hopefully there will be an explanation in the future as to why.


Gonna go on ahead and add the steps on how to create a Server Key. Just in case others are confused, or newbies are not sure how.

  1. Go to your Google Developers Console
  2. On the left-pane, click on Credentials
  3. Under the Credentials Tab, Click on Create Credentials
  4. Select API Key

---Update in the Developers Console cuts the step here---

  1. Select Server Key
  2. Fill out the details.
  3. Click on Create

The Server Key should be available by then.


Update: It seems there was a recent change when generating an API key. It is the same up until Step 4 that I mentioned above. However, after you select API Key, it will directly create an API key without asking what type of key (Server, Android, Browser, iOS) it is. It will only allow you to set some Restrictions that was visible depending on which API Key you intend to generate.


Update: When creating an API Key with No Restrictions, it is showing a Notice (Exclamation Point) that your API key is vulnerable, that's why it is highly encouraged for you to add a restriction for your API key. One post had this issue and adding a restriction was able to resolved it.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Thanks man it worked for me. This issue was driving me crazy! – JasJar Jun 14 '16 at 19:56
  • 1
    `Server Key` is no longer an option. So I selected `no restrictions` and the API Key is saying invalid. – peterept Sep 08 '16 at 00:31
  • @peterept I just checked. There is actually no option anymore. When you choose to create an API Key, it just creates one. It wasn't like that when I tried yesterday, must be recently changed. However, if you choose to add restrictions, you can choose which it can be used. I'll go look around and provide an update in my answer here. – AL. Sep 08 '16 at 01:13
  • 1
    Thanks. I use MixPanel for push notification - and the new `no restrictions` keys are not accepted into their system. – peterept Sep 08 '16 at 01:16
  • @peterept So I've done some digging and there seems to be no more types of API Key. I've reached back to the old [help doc](https://support.google.com/cloud/answer/6158862?hl=en&ref_topic=6262490) and even that was already updated. So there shouldn't be any particular changes when it comes to GCM and the keys. Two options I can think of, is for you to simply [re-generate the key](http://stackoverflow.com/a/37741826/4625829) you have now, then apply it ti your implementation. Or you can also try asking MixPanel(support@mixpanel.com), see if you're not the only one encountering this. – AL. Sep 08 '16 at 07:12
  • @AL So I now have it working - thanks to the brilliant Will at MixPanel support. These keys in GCM will no longer work. You need to go to https://console.firebase.google.com and import your existing GCM project. Then go to Project Settings -> Cloud Messaging, and get the new updated API Key. The "Sender Id" should be exactly the same number as your existing "GCM Project Number". I have confirmed PUSH now working through to the device. – peterept Sep 09 '16 at 01:50
  • Additionally, existing keys (from GSM before firebase) are working as normal. I guess Google snapped a copy of all existing keys into Firebase. So only new keys generated in GCM are invalid. – peterept Sep 09 '16 at 01:59
  • I spent an hour trying to figure out how to get the server key for GCM. Turns out that everything has to be done in the [Firebase console](https://console.firebase.google.com/) now. See [this link](https://developers.google.com/cloud-messaging/android/client) for more details. – mannykary Sep 11 '16 at 01:18
  • @mannykary If you're trying to get your API key, it's visible on both the Firebase Console and the Developer's console. However, if you intend to regenrate the API key, the steps above (until Step 4) is the way. :) The documentation you linked is for new developers wanting to use GCM, however they are advised to use FCM now. :) – AL. Sep 11 '16 at 01:51
  • @AL. Thanks for the clarification. I now see the server key in the Google Developer console, but only after I imported the project to Firebase Console. I created the project in Developer console fairly recently and I couldn't find a way to generate the server key there. Google keeps changing its Developer Console interface, so it gets very confusing especially with resources and tutorials that are providing instructions for the outdated interface. – mannykary Sep 11 '16 at 02:38
  • @mannykary Your welcome. Whenever you create a new Firebase Project, it automatically creates an API key you can use. But, as you mentioned, there still no way to generate/regenerate it from the Firebase Console. – AL. Sep 11 '16 at 12:54
  • @AL. I meant I wasn't able to generate a server key from scratch in the Developer console. The API key that is generated from scratch nowadays is not a server key. This confused me when I was trying to use it for FCM. But it is apparent that you can still *regenerate* a server key that was either created a while ago in the Developer console, or that was automatically created by Firebase. – mannykary Sep 11 '16 at 21:02
  • @AL. I am also facing the similar issue. I can no more see the option of Server Key in my GCM console. Instead I am seeing FCM option. But then also, I can create an API key in GCM. But when I use it, it throws `401 Unauthorized` exception. Can you please tell me how can I rectify this problem? – YuDroid Sep 19 '16 at 06:42
  • @AL, If I migrate my existing GCM project into FCM, will this solve the problem? – YuDroid Sep 19 '16 at 06:44
  • @YuDroid I'm not entirely sure. I've added that **Update** since I've seen some posts like [this](http://stackoverflow.com/questions/39528131) where updating to FCM fixed it on their end. I u[dated my answer again. You can first try on adding a IP address restriction. See if it works that way first. – AL. Sep 19 '16 at 07:22
  • @AL. I migrated my project from GCM to FCM and added some more information like Package name. In Firebase console, I can find the Server Key in Settings menu. In the beginning, this worked for sometime and I can send the Push Notification to my Android app. (This app is GCM based). When I received the messages, they were completely blank. But after sometime, this also stopped working. – YuDroid Sep 19 '16 at 12:59
  • 1
    It seems that other users also have this kind of problem, but Google does not have answer for this. https://groups.google.com/forum/#!topic/firebase-talk/qjCTWINQMNU – YuDroid Sep 19 '16 at 13:01
  • @YuDroid Thanks for sharing the thread. :-D With regards to your FCM project, did you also follow the proper migration for your Android App? – AL. Sep 19 '16 at 14:48
  • @AL. No I did not make any code changes/migration for my Android app. I am trying to make my existing Android app to work with FCM URL from back end. If this get completed, then it will save my lot of effort. – YuDroid Sep 20 '16 at 07:26
  • 1
    @YuDroid Seems like it should work. See [here](http://stackoverflow.com/a/39565070/4625829). So long as it's working then. Cheers. :) – AL. Sep 20 '16 at 08:06
  • 4
    @AL. So finally my Android app is working now without making any code changes. What I did was just to Import my existing GCM based project into FCM (Firebase Cloud Messaging). When I checked the project settings in FCM, it generated `Server Key`. I uploaded this `Server Key` on my back-end server (C# based) and I can sent Push Notifications. Cheers. Thanks for your time and consideration. – YuDroid Sep 20 '16 at 10:34
  • That options don't exists anymore. Now, it download a json file with some data... how to use it? Where is the server key? I'm using a browser client (web) and need send the notification from my server. Not android. – Tiago Gouvêa Sep 24 '16 at 17:29
  • @TiagoGouvêa I just tested it. The steps are still the same. Are you referring to the google-services.json? – AL. Sep 24 '16 at 19:03
  • Yes @AL. how to use the data on json? It have a private_key_id but that value don't work to... what field there are the correct? Or I need refence the file to work? – Tiago Gouvêa Sep 24 '16 at 20:37
  • 1
    I tried importing my existing project, but the legacy server key fails. All other legacy keys are working well. Any idea why? – Awani Dec 13 '16 at 13:54
  • @user2195963 By *legacy keys*, do you mean he Server Keys generated in the Developers Console? – AL. Dec 13 '16 at 14:44
  • @AL. No, the legacy key generated by the Firebase console. Firebase came up with the new server keys only a couple of weeks back. – Awani Dec 13 '16 at 16:39
  • @user2195963 - No idea. According to the note in the Firebase Console, Legacy Server Keys should still work regardless. But if the new Server Keys are now working, why not use that instead? It is the suggested value to use anyways. However, if you still prefer to use the legacy server key, I suggest to contact Firebase Support for more help on why it's not working. Cheers! – AL. Dec 14 '16 at 01:56
  • 1
    Dude u rocks! Working on c# only changing the api key! – agustinm20 Sep 20 '17 at 18:05