使用Google Cloud Firestore链接Xamarin应用程序和Google Firestore

ruyhziif  于 2023-03-27  发布在  Go
关注(0)|答案(1)|浏览(111)

我有一个Xamarin应用程序,我想连接到我的Firestore数据库。我用'插件.GoogleFirestore',但它太旧了,我可以用这个包发布我的应用程序,所以我下载了'Google.Cloud.Firebase'和'Google.Cloud.Firebase.V1'包。他们工作得很好。我用他们为我的移动的应用程序和我的Windows应用程序。为我的Windows应用程序,他们工作正常,但是我的Xamarin移动的应用程序有问题。
我无法将应用程序与数据库连接,因为系统找不到json文件。这是错误:

System.InvalidOperationException: Error reading credential file from location app-firebase-adminsdk.json: Could not find file "/app-firebase-adminsdk.json"
Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS. ---> System.IO.FileNotFoundException: Could not find file "/app-firebase-adminsdk.json"
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x001aa] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:239 
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean isAsync, System.Boolean anonymous) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:149 
  at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/mcs/class/corlib/System.IO/FileStream.cs:86 
  at (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess)
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.GetStream (System.String filePath) [0x00000] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:380 
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialFromFileAsync (System.String credentialPath, System.Threading.CancellationToken cancellationToken) [0x0002f] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:162 
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.<CreateDefaultCredentialAsync>g__GetAdcFromEnvironmentVariableAsync|11_0 () [0x0005c] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:108 
   --- End of inner exception stack trace ---
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.<CreateDefaultCredentialAsync>g__GetAdcFromEnvironmentVariableAsync|11_0 () [0x000b0] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:114 
  at Google.Apis.Auth.OAuth2.DefaultCredentialProvider.CreateDefaultCredentialAsync () [0x00037] in C:\Apiary\2022-12-01.12-06-01\Src\Support\Google.Apis.Auth\OAuth2\DefaultCredentialProvider.cs:85 
  at Google.Api.Gax.Grpc.DefaultChannelCredentialsCache.<.ctor>b__5_1 () [0x00027] in /_/Google.Api.Gax.Grpc/DefaultChannelCredentialsCache.cs:51 
  at Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions (System.Threading.Tasks.Task task) [0x00009] in /_/Google.Api.Gax/TaskExtensions.cs:51 
  at Google.Api.Gax.TaskExtensions.ResultWithUnwrappedExceptions[T] (System.Threading.Tasks.Task`1[TResult] task) [0x00000] in /_/Google.Api.Gax/TaskExtensions.cs:30 
  at Google.Api.Gax.Grpc.DefaultChannelCredentialsCache.GetCredentials () [0x00000] in /_/Google.Api.Gax.Grpc/DefaultChannelCredentialsCache.cs:67 
  at Google.Api.Gax.Grpc.ChannelPool.GetChannel (Google.Api.Gax.Grpc.GrpcAdapter grpcAdapter, System.String endpoint, Google.Api.Gax.Grpc.GrpcChannelOptions channelOptions) [0x00018] in /_/Google.Api.Gax.Grpc/ChannelPool.cs:74 
  at Google.Api.Gax.Grpc.ClientBuilderBase`1[TClient].CreateCallInvoker () [0x00034] in /_/Google.Api.Gax.Grpc/ClientBuilderBase.cs:366 
  at Google.Cloud.Firestore.V1.FirestoreClientBuilder.BuildImpl () [0x00006] in /_/apis/Google.Cloud.Firestore.V1/Google.Cloud.Firestore.V1/FirestoreClient.g.cs:414 
  at Google.Cloud.Firestore.V1.FirestoreClientBuilder.Build () [0x00001] in /_/apis/Google.Cloud.Firestore.V1/Google.Cloud.Firestore.V1/FirestoreClient.g.cs:400 
  at Google.Cloud.Firestore.FirestoreDbBuilder.Build () [0x00056] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/FirestoreDbBuilder.cs:105 
  at Google.Cloud.Firestore.FirestoreDb.Create (System.String projectId, Google.Cloud.Firestore.V1.FirestoreClient client) [0x00000] in /_/apis/Google.Cloud.Firestore/Google.Cloud.Firestore/FirestoreDb.cs:104

这是我的代码:

string path = AppDomain.CurrentDomain.BaseDirectory + @"app-firebase-adminsdk.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", path);
FirestoreDb databaseFS = FirestoreDb.Create("appmobile");

我也试过

string path = Path.Combine(FileSystem.AppDataDirectory, @"app-firebase-adminsdk.json")

而不是

string path = AppDomain.CurrentDomain.BaseDirectory + @"app-firebase-adminsdk.json";

但它不起作用..
.json文件安装在我的应用程序项目中:

  • 构建操作:嵌入式资源
  • 复制到输出目录:请勿复制

在Xamarin.Android和Xamarin.iOS中为:

  • 构建操作:内容
  • 复制到输出目录:始终复制

你知道吗?

kfgdxczn

kfgdxczn1#

您可能为.json文件设置了错误的Build Action
对于安卓系统:

  • 将任何文件添加到Android项目的Assets文件夹中,并将Build Action标记为AndroidAsset,以便与OpenAppPackageFileAsync一起使用。

对于ios:

  • 将任何文件添加到iOS项目中的Resources文件夹中,并将Build Action标记为BundledResource,以便将其与OpenAppPackageFileAsync一起使用。

相关问题