question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase and Google Maps API keys result in a wrong configuration

See original GitHub issue

Description I think the plugin extracts wrong API keys (google_api_key and google_crash_reporting_api_key) from the google-services.json file during the Android app build in a certain scenario. Possibly, the issue is partially caused by a wrong JSON file being generated by Firebase in the first place.

My Android app uses both Firebase Crashlytics and Google Maps. When I was adding the Google Maps integration, I followed the recommendations and I created dedicated API keys for Google Maps in Google APIs Console and I restricted them only to some of the app flavors by specifying the package name and SHA-1 restriction (also restricted to the “Maps SDK for Android” and “Places API”). In Firebase, both “staging” and “production” flavors are configured as separate apps in the same Firebase project, but they differ in package names and SHA-1 fingerprints.

Configuration com.google.gms:google-services version: 4.3.3 com.google.firebase:firebase-analytics-ktx version: 17.5.0 com.google.firebase:firebase-crashlytics-ktx version: 17.2.1 com.google.firebase:firebase-crashlytics-gradle version: 2.2.1

Google APIs Console apis

Google APIs Console - Google Maps key 1 key1

Google APIs Console - Google Maps key 2 key2

Firebase - staging app firebase1

Firebase - production app firebase2

The JSON file I get from Firebase:

{
  "project_info": {
    "project_number": "922450535496",
    "firebase_url": "https://[confidential].firebaseio.com",
    "project_id": "[confidential]",
    "storage_bucket": "[confidential].appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:922450535496:android:d7670fa2636a5081",
        "android_client_info": {
          "package_name": "[confidential].prod"
        }
      },
      "oauth_client": [
        {
          "client_id": "922450535496-ls60[confidential]",
          "client_type": 1,
          "android_info": {
            "package_name": "[confidential].prod",
            "certificate_hash": "ba61e96fd3ddd4c56e359df0a3e8bc689cae4a69"
          }
        },
        {
          "client_id": "922450535496-mrti[confidential]",
          "client_type": 1,
          "android_info": {
            "package_name": "[confidential].prod",
            "certificate_hash": "5e014ddba1571358998ab939a934cae353640a2d"
          }
        },
        {
          "client_id": "922450535496-obbg[confidential]",
          "client_type": 1,
          "android_info": {
            "package_name": "[confidential].prod",
            "certificate_hash": "133e5c7fc7e7ff068b7227a20684c659bfd5a4ca"
          }
        },
        {
          "client_id": "922450535496-d585[confidential]",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyCkOh[confidential]SSVSVSAz00"
        },
        {
          "current_key": "AIzaSyAdVW[confidential]1dzU9qvu3k"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "922450535496-d585[confidential]",
              "client_type": 3
            }
          ]
        }
      }
    },
    {
      "client_info": {
        "mobilesdk_app_id": "1:922450535496:android:02f104bc77b0e115",
        "android_client_info": {
          "package_name": "[confidential].stage"
        }
      },
      "oauth_client": [
        {
          "client_id": "922450535496-ufah[confidential]",
          "client_type": 1,
          "android_info": {
            "package_name": "[confidential].stage",
            "certificate_hash": "b3645599ddfb1957a011c79dc5706d78e90f6ea0"
          }
        },
        {
          "client_id": "922450535496-ha7n[confidential]",
          "client_type": 1,
          "android_info": {
            "package_name": "[confidential].stage",
            "certificate_hash": "a26044bd4dbfe67e0f6fa052ca553a6b70d6e37e"
          }
        },
        {
          "client_id": "922450535496-vi8b[confidential]",
          "client_type": 1,
          "android_info": {
            "package_name": "[confidential].stage",
            "certificate_hash": "133e5c7fc7e7ff068b7227a20684c659bfd5a4ca"
          }
        },
        {
          "client_id": "922450535496-d585[confidential]",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyCkOh[confidential]SSVSVSAz00"
        },
        {
          "current_key": "AIzaSyAdVW[confidential]1dzU9qvu3k"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "922450535496-d585[confidential]",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

As you can see, the API keys in the api_key array are only the Google Maps keys and there is no Android key (auto created by Firebase) key.

Before I added Google Maps API keys in Google APIs Console, that part of the google-services.json file was different as it contained the Android key (auto created by Firebase) key.

      "api_key": [
        {
          "current_key": "AIzaSyBtnw[confidential]le0_4ek0GM"
        }
      ],

Generated resource

Now, when I build the app, it generates the resource file app/build/generated/res/google-services/stage/debug/values/values.xml with the following content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="default_web_client_id" translatable="false">922450535496-d585[confidential]</string>
    <string name="firebase_database_url" translatable="false">https://[confidential].firebaseio.com</string>
    <string name="gcm_defaultSenderId" translatable="false">922450535496</string>
    <string name="google_api_key" translatable="false">AIzaSyCkOh[confidential]SSVSVSAz00</string>
    <string name="google_app_id" translatable="false">1:922450535496:android:02f104bc77b0e115</string>
    <string name="google_crash_reporting_api_key" translatable="false">AIzaSyCkOh[confidential]SSVSVSAz00</string>
    <string name="google_storage_bucket" translatable="false">[confidential].appspot.com</string>
    <string name="project_id" translatable="false">[confidential]</string>
</resources>

As you can see, one of the API keys I created just for Google Maps APIs (AIzaSyCkOh[confidential]SSVSVSAz00) is used in both google_api_key and google_crash_reporting_api_key. Moreover, that API key is restricted to a different package name and SHA-1 than I just built, which seems even worse 🤦

Authorization issue When I run the app, I see the following warnings and errors in the logcat:

09-11 11:14:00.743 5210-5247/[confidential].stage W/Firebase-Installations: Error when communicating with the Firebase Installations server API. HTTP response: [403 Forbidden: {
      "error": {
        "code": 403,
        "message": "Requests from this Android client application [confidential].stage are blocked.",
        "status": "PERMISSION_DENIED",
        "details": [
          {
            "@type": "type.googleapis.com/google.rpc.Help",
            "links": [
              {
                "description": "Google developer console API key",
                "url": "https://console.developers.google.com/project/922450535496/apiui/credential"
              }
            ]
          }
        ]
      }
    }
    ]
09-11 11:14:00.743 5210-5247/[confidential].stage W/Firebase-Installations: Firebase options used while communicating with Firebase server APIs: AIzaSyCkOh[confidential]SSVSVSAz00, [confidential], 1:922450535496:android:02f104bc77b0e115
09-11 11:14:00.743 5210-5247/[confidential].stage 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.

Surprisingly, crash reporting seems to work nonetheless as I can see a crash I induced myself in the dashboard, but I have no clue why it works, especially considering the logs above.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:15
  • Comments:8

github_iconTop GitHub Comments

1reaction
martin-brauncommented, Apr 26, 2022

@dwidc Then the API key linking between Google and Firebase is broke on your project like it is on ours and I think they will have hard times to fix it (based on my gut feeling when I was reading their answers on my request). You might be able to fix it yourself by re-creating the Firebase project or following the quote I did above (which didn’t work for me). The issue was fixed on Mar 11 for new projects, thus new links between Google and Firebase.

If you can, I recommend you to just not deal with this issue. Grab your json/plist and fix the API keys within it by hand and never look back. This is what I did in the peaceful mind that it will never happen for new Firebase projects, again.

0reactions
dwidccommented, Apr 26, 2022

@martin-braun It’s a relatively new Firebase and Google Maps project, created about 2-3 months ago.

Thanks for info, I’ll file report to Firebase later.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Learn about using and managing API keys for Firebase - Google
An API key is a unique string that's used to route requests to your Firebase project when interacting with Firebase and Google services....
Read more >
Error Messages | Maps JavaScript API - Google Developers
This behavior typically indicates issues with either an API key or billing. In order to use Google Maps Platform products, billing must be...
Read more >
Something about Google API keys, how to secure them, and ...
For this app, we want to use the Google Maps Geocoding API. To get the information we need, we have to do a...
Read more >
APIs and billing - API Console Help - Google Help
Some Google APIs charge for usage, and you need to enable billing before you can start using these APIs. Enabling billing for the...
Read more >
Authenticate using API keys - Google Cloud
Google Cloud · Overview. close. Accelerate your digital transformation; Whether your business is early in its journey or well on its way to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found