簡體   English   中英

如何將我的 MAUI 應用程序連接到 Firestore 數據庫? (通過服務帳戶 json 文件)

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

創建一個 MAUI 應用並嘗試將其連接到 Firestore。 我正在按照此處的建議下載服務帳戶 JSON 文件並設置GOOGLE_APPLICATION_CREDENTIALS環境變量。

我從我的 firebase 項目在線下載了我的服務文件,將其重命名為GoogleAppCredentials.json ,然后使用構建操作“MauiAsset”將其包含在我的 MAUI 項目中,如下所示:

GoogleAppCredentials 位置

然后我的 cs 文件引用該文件:

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

但是當我在 Android Emulator 上運行該應用程序時,出現以下錯誤:

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)

在 Windows 機器上...

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)

那么使用 MAUI,我如何正確地將GoogleAppCredentials.json復制到應用程序中(如果不是 MauiAsset,還有什么?),然后在.cs文件中引用它?

創建一個 MAUI 應用並嘗試將其連接到 Firestore。 我正在按照此處的建議下載服務帳戶 JSON 文件並設置 GOOGLE_APPLICATION_CREDENTIALS 環境變量。

此 .NET 客戶端用於后端服務。 您可以安裝它並開始運行,但它們旨在用於 Google Cloud 環境中的 .net 服務。

MAUI 是一個多目標項目,它增加了在項目中保存所有代碼庫的能力,因為它在 Xamarin 中為每個平台添加了一個單獨的項目。 多目標項目竊取是針對具有平台綁定的特定平台工件構建的。 例如, net6.0-android包含與所有標准 Android 生態系統的 android 綁定。

要與 Firebase Firestore 集成,您需要使用 Android 指南和庫:Xamarin.Firebase.Firestore 並使用 GoogleServicesJson 構建操作配置 google-services.json,但在 MAUI 中,您需要在 Platform/Andoroid 位置執行此操作。

我的 csproj 包含此配置

<ItemGroup>
    <None Remove="Platforms\Android\google-services.json" />
</ItemGroup>
<ItemGroup>
    <GoogleServicesJson Include="Platforms\Android\google-services.json" />
</ItemGroup>

對於我的項目,我不針對 WinUI,所以我使用這個 Lib 來訪問 Firestore Plugin.CloudFirestore

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM