我已经尝试了许多方法,比如在几个网站上,但没有成功,我试图使用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实时数据库,我能够连接它。
发布于 2019-05-22 20:26:45
据我所知,你不能在设备上的应用程序中使用默认凭据(GOOGLE_APPLICATION_CREDENTIALS),因为它无法找到位于你PC上的文件的路径。我已经找到了这段代码,我认为它应该可以工作(但到目前为止,我还没有成功地使用它-我得到了一个exeption,所以我希望我的答案可以帮助激励其他人找到解决方案)
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:在C:\Users\rad\Documents\Xamarin projects\Padlelogg 2.10\Padlelogg\Data\DataHandler.cs:360 }位置的T:\src\github\google-cloud-dotnet\releasebuild\apis\Google.Cloud.Firestore.V1\Google.Cloud.Firestore.V1\FirestoreClient.cs:622中,Google.Cloud.Firestore.V1.FirestoreClientImpl类型的VTable安装失败(Grpc.Core.Channel频道,Google.Cloud.Firestore.V1.FirestoreSettings设置) 0x0000c
这个例外我到目前为止还没能解决。
https://stackoverflow.com/questions/55062933
复制相似问题