Firebase凭证文件在Maui macOS应用中

huangapple go评论74阅读模式
英文:

Firebase credentials file on maui macos app

问题

The result:
TestFirebase[1325:25275] 连接到 Firestore 时出错:从位置 /Users/*************/Projects/TestFirebase/TestFirebase/bin/Debug/net7.0-maccatalyst/maccatalyst-x64/TestFirebase.app/Contents/MonoBundle/giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json 读取凭证文件时出错:找不到文件 '/Users/**************/Projects/TestFirebase/TestFirebase/bin/Debug/net7.0-maccatalyst/maccatalyst-x64/TestFirebase.app/Contents/MonoBundle/giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json'。

TestFirebase[1325:25275] 请检查环境变量 GOOGLE_APPLICATION_CREDENTIALS 的值。

我不清楚是在编译时未复制文件(在 Windows 上是复制的,但在 macOS 上没有),还是寻找文件夹的方式不正确。感谢能帮助我的任何人。

英文:

Good morning. I am developing a net maui app but I am experiencing the following problem: On windows it works perfectly, on mac I can not connect to the database as I can not trace the file with firebase credentials.

string basePath = AppDomain.CurrentDomain.BaseDirectory;
string credentialsPath = Path.Combine(basePath, "giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json");

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialsPath);

       try
        {
            FirestoreDb db = FirestoreDb.Create("giogo-dee69");
		DocumentReference coll = db.Collection("NomeCollezione").Document("Capoccione");

        Modello DaInserire = new Modello();

        DaInserire.Username = "Picciotto";
        DaInserire.NumeroCapelli = 19;

	await coll.SetAsync(DaInserire);

        }
        catch (Exception ex)
        {
            Console.WriteLine("Errore di connessione a Firestore: " + ex.Message);
        }



The result:
TestFirebase[1325:25275] Errore di connessione a Firestore: Error reading credential file from location /Users/*************/Projects/TestFirebase/TestFirebase/bin/Debug/net7.0-maccatalyst/maccatalyst-x64/TestFirebase.app/Contents/MonoBundle/giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json: Could not find file '/Users/**************/Projects/TestFirebase/TestFirebase/bin/Debug/net7.0-maccatalyst/maccatalyst-x64/TestFirebase.app/Contents/MonoBundle/giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json'.

TestFirebase[1325:25275] Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS.

I don't understand if it does not copy the file at the time of compilation (with Windows it does and macOS does not) or if it is wrong way to find the folder with the file. Thanks to anyone who can help me.

答案1

得分: 0

这与这个问题相似:在Windows上设置GOOGLE_APPLICATION_CREDENTIALS的SetEnvironmentVariable,在Android上会出错。mattleibow的评论 提供了一个跨平台的解决方案,可能会有帮助。

首先,在Raw子文件夹中设置凭据文件,并设置属性 "MauiAsset" 和 "copy if new"。

然后在运行时读取文件并存储在临时文件夹或其他文件夹中:

//我们将要复制文件的新路径。
var newPath = Path.Combine(FileSystem.CacheDirectory, "giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json");

//以这种方式在Resource/Raw中读取文件
using var json = await FileSystem.OpenAppPackageFileAsync("giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json");
//创建新路径并将原始的json文件复制到这里
using var dest = File.Create(newPath);
await json.CopyToAsync(dest);

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", newPath);

还要使用 dest.Close();,如 MahmoudAlEssawi 的评论 所述。

然后,您可以在新路径中找到凭据文件,然后我们可以读取它。

希望能够成功!

英文:

That's similar to this issue: SetEnvironmentVariable for GOOGLE_APPLICATION_CREDENTIALS works on windows but give error on android. and mattleibow's comment gives a solution on cross platform which might be help.

First, set the credentials file in Raw sub-folder and set property "MauiAsset" and "copy if new".

Then read the file at runtime and store in a temp folder or any other folder:

//new path we will copy the file to. 
var newPath = Path.Combine(FileSystem.CacheDirectory, "giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json");

// read the file in Resource/Raw in this way
using var json = await FileSystem.OpenAppPackageFileAsync("giogo-dee69-firebase-adminsdk-fsksp-1cf7a95106.json");
//create the new path and copy the original json file here
using var dest = File.Create(newPath);
await json.CopyToAsync(dest);

Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", newPath);

Also, use dest.Close(); as MahmoudAlEssawi comment.

Then you may find the credential file in new path and we could read it.

Hope it works!

huangapple
  • 本文由 发表于 2023年6月6日 04:04:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定