到目前为止,将examples与.net一起使用Google Cloud Firestore都表明您使用以下命令连接到Firestore数据库:
FirestoreDb db = FirestoreDb.Create(projectId);
但这是否跳过身份验证步骤?我似乎找不到找到使用Google服务帐户进行连接的示例。我猜您需要使用服务帐户的private_key / private_key_id / client_email连接吗?
答案 0 :(得分:5)
我无法编译@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" />
答案 1 :(得分:4)
但这会跳过身份验证步骤吗?
不。它将使用默认的应用程序凭据。如果您在Google Cloud Platform(AppEngine,GCE或GKE)上运行,它们将只是实例的默认服务帐户凭据。否则,您应该设置GOOGLE_APPLICATION_CREDENTIALS
环境变量以引用服务帐户凭据文件。
在您提到的home page of the user guide中:
在Google Cloud Platform上运行时,无需采取任何措施进行身份验证。
否则,验证API调用的最简单方法是下载服务帐户JSON文件,然后将GOOGLE_APPLICATION_CREDENTIALS环境变量设置为引用它。凭据将自动用于身份验证。有关更多详细信息,请参见“身份验证入门”指南。
使用非默认凭据会比较尴尬; this recent issue举例说明。
答案 2 :(得分:2)
您还可以使用存储在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);
答案 3 :(得分:0)
这对我有用。
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");