我尝试了多种方法,就像在几个站点中一样,但是没有运气,我尝试使用Google.Cloud.Firestore
和Google.Apis.Storage.v1
Nuget软件包进行连接。代码如下。
Google.Cloud.Firestore.FirestoreDb db = Google.Cloud.Firestore.FirestoreDb.Create("test");
CollectionReference collection = db.Collection("users");
DocumentReference document = await collection.AddAsync(new { email = "xamemail@12", name = "xamemail" });
当我尝试此代码时,发生了一个异常,例如未设置环境变量GOOGLE_APPLICATION_CREDENTIALS,然后在Windows系统以及下面的代码中设置了GOOGLE_APPLICATION_CREDENTIALS。
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"C:\\path-to-json", EnvironmentVariableTarget.Machine);
它显示了另一个错误,即在路径中找不到文件,但文件在那里,我设置了路径许可权。
如果有人在那里提供帮助,有人已经在使用xamarin-您项目中的cloud firestore db吗?
请注意,它不是firebase实时数据库,我可以连接它。
答案 0 :(得分:0)
据我了解,您无法在设备上的应用中使用默认凭据(GOOGLE_APPLICATION_CREDENTIALS),因为它无法找到PC上文件的路径。 我已经找到了我认为应该可以使用的代码(但是到目前为止,我还没有成功实现它-我得到了认可,所以我希望我的回答可以帮助激发其他人为此找到解决方案)< / p>
using Google.Cloud.Firestore;
using Google.Cloud.Firestore.V1;
using Google.Apis.Auth.OAuth2;
using Grpc.Auth;
using Grpc.Core;
...
//First Pack your Jason fil into a Jason string.
//Fore security reasons I'm not sure this is a good idea, but it is what I could think of
string jsonCred = @”{ Your Json cred file (Replace “ with ‘) }”;
// Get you credential. As far as I have understood it must be scoped.
var credential = GoogleCredential.FromJson(jsonCred).CreateScoped(FirestoreClient.DefaultScopes);
// Create a channel and add the channel to the Firestore client
Channel channel = new Channel(FirestoreClient.DefaultEndpoint.Host, FirestoreClient.DefaultEndpoint.Port, credential.ToChannelCredentials());
FirestoreClient client = FirestoreClient.Create(channel);
// Then I think it should be possible to call.
FirestoreDb db = FirestoreDb.Create(projectId, client);
但是到目前为止,我在排队:
FirestoreClient client = FirestoreClient.Create(channel):
我收到此异常:
System.TypeLoadException:类型为Google.Cloud.Firestore.V1.FirestoreClientImpl的VTable设置在Google.Cloud.Firestore.V1.FirestoreClient.Create(Grpc.Core.Channel通道,Google.Cloud.Firestore.V1.FirestoreSettings)失败设置)[0x0000c]位于Padlelogg.DataHandler.SaveToFireStore的T:\ src \ github \ google-cloud-dotnet \ releasebuild \ apis \ Google.Cloud.Firestore.V1 \ Google.Cloud.Firestore.V1 \ FirestoreClient.cs:622中[T](System.String集合,System.Collections.Generic.List`1 [T]数据列表)[0x00072]在C:\ Users \ rad \ Documents \ Xamarin projects \ Padlelogg 2.10 \ Padlelogg \ Data \ DataHandler.cs中: 360}
到目前为止,我无法解决此异常