繁体   English   中英

如何使用 .net 核心连接到 Cloud Firestore DB?

[英]How to connect to Cloud Firestore DB with .net core?

到目前为止,所有将 Google Cloud Firestore 与 .net 结合使用的示例都表明您使用以下命令连接到 Firestore 数据库:

FirestoreDb db = FirestoreDb.Create(projectId);

但这是否跳过了身份验证步骤? 我似乎无法找到连接它以使用 Google 服务帐户的示例。 我猜您需要使用服务帐户的 private_key/private_key_id/client_email 进行连接?

您还可以使用存储在 json 文件中的凭据:

    GoogleCredential cred = GoogleCredential.FromFile("credentials.json");
    Channel channel = new Channel(FirestoreClient.DefaultEndpoint.Host,
                  FirestoreClient.DefaultEndpoint.Port,
                  cred.ToChannelCredentials());
    FirestoreClient client = FirestoreClient.Create(channel);
    FirestoreDb db = FirestoreDb.Create("my-project", client);

我无法编译@Michael Bleterman 的代码,但是以下对我有用:

using Google.Cloud.Firestore;
using Google.Cloud.Firestore.V1;


var jsonString = File.ReadAllText(_keyFilepath);
var builder = new FirestoreClientBuilder {JsonCredentials = jsonString};
FirestoreDb db = FirestoreDb.Create(_projectId, builder.Build());

我使用的软件包:

<PackageReference Include="Google.Cloud.Firestore" Version="2.0.0-beta02" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.5.0" />

但这是否跳过了身份验证步骤?

否。它将使用默认应用程序凭据。 如果您在 Google Cloud Platform(AppEngine、GCE 或 GKE)上运行,它们将只是实例的默认服务帐户凭据。 否则,您应该设置GOOGLE_APPLICATION_CREDENTIALS环境变量以引用服务帐户凭据文件。

从您提到的用户指南主页

在 Google Cloud Platform 上运行时,无需采取任何操作来进行身份验证。

否则,验证 API 调用的最简单方法是下载服务帐户 JSON 文件,然后设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量以引用它。 凭据将自动用于身份验证。 有关更多详细信息,请参阅身份验证入门指南。

使用非默认凭据有点尴尬; 最近的这个问题给出了一个例子。

这对我有用。

https://pieterdlinde.medium.com/netcore-and-cloud-firestore-94628943eb3c

string filepath = "/Users/user/Downloads/user-a4166-firebase-adminsdk-ivk8q-d072fdf334.json";
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", filepath);
fireStoreDb = FirestoreDb.Create("user-a4166");

最简单的方法:

获取服务帐户 json 文件并将值硬编码为 class:

    public class FirebaseSettings
    {
        [JsonPropertyName("project_id")]
        public string ProjectId => "that-rug-really-tied-the-room-together-72daa";
    
        [JsonPropertyName("private_key_id")]
        public string PrivateKeyId => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
        // ... and so on
    }

添加Startup.cs

var firebaseJson = JsonSerializer.Serialize(new FirebaseSettings());

services.AddSingleton(_ => new FirestoreProvider(
    new FirestoreDbBuilder 
    { 
        ProjectId = firebaseSettings.ProjectId, 
        JsonCredentials = firebaseJson // <-- service account json file
    }.Build()
));

添加包装器FirebaseProvider

public class FirestoreProvider
{
    private readonly FirestoreDb _fireStoreDb = null!;

    public FirestoreProvider(FirestoreDb fireStoreDb)
    {
        _fireStoreDb = fireStoreDb;
    }

    // ... your methods here

}

这是通用提供程序的完整示例。

https://dev.to/kedzior_io/simple.net-core-and-cloud-firestore-setup-1pf9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM