'How do I connect my MAUI app to a Firestore database? (via service account json file)

Creating a MAUI app and trying to connect it to the Firestore. I'm following the advice here to download a service account JSON file and set the GOOGLE_APPLICATION_CREDENTIALS environment variable.

I downloaded my service file from my firebase project online, renamed it to GoogleAppCredentials.json, and then included it in my MAUI project with the Build Action "MauiAsset" like so:

GoogleAppCredentials location

My cs file then references the file:

string path = "GoogleAppCredentials.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);

But when I run the app on Android Emulator I get the following error:

System.AggregateException
  Message=One or more errors occurred. (Error reading credential file from location GoogleAppCredentials.json: Could not find file '/GoogleAppCredentials.json'.
Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS)

And on Windows Machine...

System.AggregateException
  Message=One or more errors occurred. (Error reading credential file from location GoogleAppCredentials.json: Could not find file 'C:\WINDOWS\system32\GoogleAppCredentials.json'.
Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS)

So with MAUI, how do I properly copy GoogleAppCredentials.json into the app (if not MauiAsset, what else?) and then reference it in the .cs file?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source