[英]How do I connect my MAUI app to a Firestore database in production?
I'm creating a MAUI app and I'm trying to connect it to Firestore.我正在创建一个 MAUI 应用程序,并尝试将其连接到 Firestore。 I have set everything up in Firebase and I can successfully connect to the Firestore by setting the environment variable: "GOOGLE_APPLICATION_CREDENTIALS" on my windows machine locally.我已经在 Firebase 中设置了所有内容,并且可以通过在本地 windows 机器上设置环境变量:“GOOGLE_APPLICATION_CREDENTIALS”来成功连接到 Firestore。
However, I cannot make it work if I test out the app using my local android device since this device doesn't have the environment variable set up.但是,如果我使用本地 android 设备测试应用程序,我无法使其工作,因为该设备没有设置环境变量。
So my question is, how would I go about doing this, so I can connect to the Firestore on my android device?所以我的问题是,我将如何 go 这样做,所以我可以连接到我的 android 设备上的 Firestore?
Some thoughts:一些想法:
I would imagine that I need to set the environment variable in the MAUI code and somehow read it, but wouldn't that expose the secret file since it would now exist inside the MAUI project?我想我需要在 MAUI 代码中设置环境变量并以某种方式读取它,但这不会暴露秘密文件,因为它现在存在于 MAUI 项目中吗?
It looks like some people are using the google-services.json file for this purpose (which is not strictly secret) so if I could use that instead, then I guess that would be the best, but again, how would I do that?看起来有些人为此目的使用了 google-services.json 文件(这不是严格保密的),所以如果我可以使用它,那么我想那将是最好的,但我又该怎么做呢?
I have tried adding the files in the Asset files, .csproj file, setting environment variables in the MainProgram etc. but I just can't make it work and even worse, I'm unsure whether that approach would be insecure.我尝试在资产文件、.csproj 文件中添加文件,在 MainProgram 中设置环境变量等,但我无法让它工作,更糟糕的是,我不确定这种方法是否不安全。
Any help would be greatly appreciated.任何帮助将不胜感激。
Possible duplicate: How do I connect my MAUI app to a Firestore database?可能重复: 如何将我的 MAUI 应用程序连接到 Firestore 数据库? (via service account json file) (通过服务帐户 json 文件)
I just still don't understand how they made it work.我只是仍然不明白他们是如何让它工作的。
This is how I successfully set it up in .NET Maui:这就是我在 .NET Maui 中成功设置它的方式:
public async Task<FirestoreDb> initFirestore()
{
try
{
var stream = await FileSystem.OpenAppPackageFileAsync(Constants.FirestoreKeyFilename);
var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
FirestoreClientBuilder fbc = new FirestoreClientBuilder { JsonCredentials = contents };
return FirestoreDb.Create(Constants.FirestoreProjectID, fbc.Build());
}
catch (Exception)
{
throw;
}
}
Hope that helps.希望有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.