34

I am getting an Error Message on App Startup Stating Logs like

W/Firebase-Installations: Error when communicating with the Firebase Installations server API. HTTP response: [400 Bad Request: {
  "error": {
    "code": 400,
    "message": "API key not valid. Please pass a valid API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console",
            "url": "https://console.developers.google.com"
          }
        ]
      }
    ]
  }
}
]
2020-04-27 12:42:34.621 22226-23596/in.co.androidapp.g7 E/Firebase-Installations: Firebase Installations can not communicate with Firebase server APIs due to invalid configuration. Please update your Firebase initialization process and set valid Firebase options (API key, Project ID, Application ID) when initializing Firebase.

I got an email about a week earlier that I should update my google_services.json File, which I have done 4-5 times. No Improvements. It had been working fine for around a year. Its been only 2-3 days since I am facing this issue in the app.

Subsequently, Firebase Cloud Messaging and other firebase services are not working. I am not doing programmatic initialization (that is, using a FirebaseOptions object to provide these values), just the default initialization using FirebaseApp.initializeApp(this);

I have tried https://github.com/firebase/firebase-android-sdk/blob/master/firebase-installations/API_KEY_RESTRICTIONS.md

Thanks in Advance.

Daksh Agrawal
  • 855
  • 1
  • 11
  • 22
  • is your package name correct in firebase console? – NullByte08 Apr 27 '20 at 07:36
  • @NullByte08 Yeah, and it has been working nicely for around an year – Daksh Agrawal Apr 27 '20 at 07:38
  • To further analyze this issue, we would need to have a look at your API key. Can you please share the API key and replace maybe 10 characters in the middle with some replacement character (e.g. "X") without changing the length of the key? – Andreas Rayo Kniep Apr 27 '20 at 21:22
  • @AndreasRayoKniep AIzaSyCrf5T0QyCUABJhZTsks7ep%%%%%%%%%%Y – Daksh Agrawal Apr 28 '20 at 04:58
  • @DakshAgrawal: That looks like a valid API key and I am confused how you can get "API key not valid." as server response. I have the feeling that the actual request to the server does not use this API key - either uses a different (invalid) API key or does not have access to this API key somehow. – Andreas Rayo Kniep Apr 28 '20 at 15:51
  • @DakshAgrawal: Out of curiosity: Did you have to create a new project to fix this issue? – Andreas Rayo Kniep May 07 '20 at 02:25
  • @AndreasRayoKniep No. Did not create a new project. I just created a new API key. The error message is still shown but FCM and other services are working well. – Daksh Agrawal May 07 '20 at 10:02

17 Answers17

40

I had this same issue. I solved it by the following way.

go to app/build/generated/res/google-services/debug/values/values.xml

Here you can find your old API key.

Replace both google_api_key and google_crash_reporting_api_key values with the new one that you can find from the google-services.json file. it will be under api-key array.

Cheers!

Abhijith Brumal
  • 1,652
  • 10
  • 14
26

If you have issues with your API key, you can create a new API key in the Cloud Console:

  • go to the Google Cloud Console
  • choose the relevant project (i.e. the project you use for your application)
  • open the menu and go to APIs & ServicesCredentials
  • on top of the page click on + CREATE CREDENTIALSAPI key
  • replace the API key in your application with the newly created API key

In case you are using the google-services.json config file from your Firebase Console, you first have to delete or restrict the API key used in your current google-services.json in order to make Firebase update config file and use a new API key.

  • Identify the API key in your google-services.json config file.
  • Confirm that the API key is creating the erroneous requests by checking its usage against the Firebase Installations API metrics page. The column Usage with this service of your API key should show a number greater than 0.
  • Delete that API key by clicking the bin symbol or add Application restrictions to that API key by clicking the pencil symbol. !!Warning!! Do not delete an API key that existing installations of your applications require for other Firebase services like Firebase Auth or Realtime-Database.

Wait a couple of minutes for Google servers to update. The next download of your google-service.json config file should contain a new API key.

You can test your configuration with the following CURL command. What is the error you are getting? (Note: If you are seeing JSON data the request was and your configuration is successful)


Test if your configuration works with the following CURL command:

api_key=<YOUR_API_KEY>
project_identifier=<YOUR_PROJECT_ID>
app_id=<YOUR_FIREBASE_APP_ID_EXAMPLE_1:12345678:android:00000aaaaaaaa>

curl -H "content-type: application/json" -d "{appId: '$app_id', sdkVersion: 't:1'}" https://firebaseinstallations.googleapis.com/v1/projects/$project_identifier/installations/?key=$api_key

Other relevant links regarding API keys and the Firebase Installations API:

Andreas Rayo Kniep
  • 5,674
  • 2
  • 30
  • 30
  • For some reason I keep getting this error, and even though "installation" is enabled (via here: https://console.cloud.google.com/apis/library/firebaseinstallations.googleapis.com ) , and even though I tried to generate a new key (even including "installation" there) and copied it to the new json file, I still get this issue. How come? – android developer Sep 02 '20 at 09:38
  • You can test your configuration with the following CURL command. What is the **error** you are getting? (Note: If you are seeing JSON data the request was and your configuration is successful) ``` api_key=; project_identifier=; app_id=; curl -H "content-type: application/json" -d "{appId: '$app_id', sdkVersion: 't:1'}" https://firebaseinstallations.googleapis.com/v1/projects/$project_identifier/installations/?key=$api_key; ``` – Andreas Rayo Kniep Sep 16 '20 at 22:29
  • In my case I encountered the exception when I replaced my old firebase android app with a new one. I have done all steps but still face that error. Then I release that I forgot to make some changes with the new app name. If you have created a new app, make sure you have replaced your old app name with the new one in your application. – genericUser Jan 24 '21 at 13:51
  • 1
    That Curl command is an amazing way to debug! Thanks. Just a couple of notes where I was slipping up, be sure to remove the semi colon (;) at the end of the curl command above! Also you need to have the Firebase Installations API enabled (silly mistake but easily done!) – FrodmanG Nov 09 '21 at 14:53
8

Late but might be helpful to others.

I tried everthing here, at last I got it working by making sure following steps,

Rule: App's SHA-1 should be registerd in Google Developer Console with the API keys used.

  1. Look for the SHA-1 you are using (debug or release). You can do it by going to android studio > Gradle form side bar > Tasks > android > signingReport. Double click on it to run. Once it runs, you will be able to find out SHA-1 for your variant. (In my case, I was trying to make it run for release variant). Keep that SHA-1 aside we will check for its existance on Google Developers Console.

  2. Now head over to https://console.cloud.google.com/apis/credentials, In your selected project > Credentials, Find API key that is used, Click on it to go into details, Find Restrict usage to your Android apps, see if the SHA-1 is registed there along with your package name.

If you do not find the same SHA-1 that you had in your project, then you can click on Add an Item to add the app. Pass package name and SHA-1 and submit.

Try again to connect firebase APIs after 5-10 mins.

enter image description here

Dharman
  • 30,962
  • 25
  • 85
  • 135
Palak Darji
  • 1,084
  • 18
  • 28
2

I encountered with this error when initializing new app so it may not solve your issue since you say your app is existing but I hope it helps.

I've created new SHA Key for SHA-1 fingerprint of keystore certificate (both for debug and signing! only signing didn't work)

and I've added them into my firebase settings (below my package name careful if you have multiple). Now it works for me.

Emre Akcan
  • 982
  • 10
  • 27
2

It turned out that the issue was with the version of firebase-messaging for me, using this version in app gradle got rid of the error:

implementation "com.google.firebase:firebase-messaging:20.1.0"
Tosin John
  • 494
  • 5
  • 15
2

This error could happen if you include in gradle some firebase modules that not configured properly (analytics, storage, messaging etc.)

Psijic
  • 743
  • 7
  • 20
2

I encountered the same issue, click on the link provided:

"links": [
          {
            "description": "Google developers console",
            "url": "https://console.developers.google.com"
          }

From there on, Go to the 'credentials' menu.

Confirm in your google-services.json file the API key being used.

Go back to the google portal and look for that key under API keys. Once you find it, click to edit and when editing, add your project's package name and SHA-1 certificate key.

Save and wait for 5 mins, the error should be gone.

MrBrushy
  • 680
  • 3
  • 17
2

I had the same issue. Please find the following steps i followed to resolve this.

  1. Navigate to the google project and make sure your app sha1 key is configured properly.
  2. Download the google-services.json file from the project and make sure it is same as the one exists in the android project
  3. Clean the project to remove all build configurations generated previously.
  4. Build the app
1

Just Register your app in the Firebase console again. and get the googl-service.json file and replace it with old. it will be solve

Sana Ebadi
  • 6,656
  • 2
  • 44
  • 44
1

If you have changed your API Key on https://console.cloud.google.com or you have restricted it you gotta follow these steps:

  1. Make sure that your new key is into google-service.json(download from firebase console).

  2. Open the google-service.json, find api_keys and leave just the current_key that belongs to every environment(dev or prod).

  3. Go to project/app and delete the folder build.

Moises Apaza Q
  • 131
  • 2
  • 4
0

To fix this error read this file using CMD or Windows Explorer

PS C:\Users\Any> keytool -keystore C:\Users\Any\.android\debug.keystore -list -v

Enter keystore password:

Use 'KeyStore Explorer' from https://keystore-explorer.org/downloads.html

& paste it here: https://console.developers.google.com/apis/credentials/key/

Nazarii Moshenskiy
  • 1,802
  • 2
  • 16
  • 35
K D
  • 205
  • 3
  • 10
0

If you are facing this issue in the Flutter app then go to your project directory and delete the build folder. Hopefully, it will work.

Abdul Mateen
  • 1,139
  • 1
  • 14
  • 21
0

Here's what made that error go away for my app: I deleted the Project .gradle folder, the Project build folder and the app build folder, all at once. I then re-installed the app, and no more Firebase Installations server API error!

kevincodes_
  • 219
  • 4
  • 4
0

This problem face me and I fixed by go to Google cloud platform and credentials and do API for firebase with Application restrictions or API restrictions as you used.jhvghvj

Community
  • 1
  • 1
0

Inside Google API's & Service Credentials > Android key (auto created by Firebase) > API Restrictions

Select All services and save. After 5-10 minute you can check.

0

This is mainly happened by your outdated google-services.json file. because sometime you may enable some extra services in firebase has changed the entire structure of that file. so go to your firebase console -> project settings -> download google-services.json and replace it

then rebuild your app. Now you check your firebase has error

Karthikeyan Ganesan
  • 1,901
  • 20
  • 23
0

I had this issue in Iran. after running a VPN my app was connected to firebase.

Ali Bagheri
  • 3,068
  • 27
  • 28