首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何将xamarin android应用连接到Cloud Firestore数据库

如何将xamarin android应用连接到Cloud Firestore数据库
EN

Stack Overflow用户
提问于 2019-03-08 20:07:23
回答 1查看 895关注 0票数 1

我已经尝试了许多方法,比如在几个网站上,但没有成功,我试图使用Google.Cloud.FirestoreGoogle.Apis.Storage.v1 Nuget包连接它。代码如下所示。

代码语言:javascript
代码运行次数:0
运行
复制
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实时数据库,我能够连接它。

EN

回答 1

Stack Overflow用户

发布于 2019-05-23 04:26:45

据我所知,你不能在设备上的应用程序中使用默认凭据(GOOGLE_APPLICATION_CREDENTIALS),因为它无法找到位于你PC上的文件的路径。我已经找到了这段代码,我认为它应该可以工作(但到目前为止,我还没有成功地使用它-我得到了一个exeption,所以我希望我的答案可以帮助激励其他人找到解决方案)

代码语言:javascript
代码运行次数:0
运行
复制
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);

但到目前为止,我在队伍中:

代码语言:javascript
代码运行次数:0
运行
复制
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

这个例外我到目前为止还没能解决。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55062933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档