Jack Fox
Jack Fox

Reputation: 61

Firestore .NET client library authentication in Azure Function

I am using Google.Cloud.Firestore version 1.0.0-beta22 in a C# Azure Function app. The documentation states to create a service account json file and create a GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to this json file.

My issue is I don't want to add the json file to my source repository, and even if I did I don't know the path to the directory that the Azure Function will be running in, thus I am unable to set the environment variable.

Is there a way to pass the credentials to the Firestore client library, or any other workarounds to address this?

Upvotes: 1

Views: 1399

Answers (2)

Bipn Paul
Bipn Paul

Reputation: 1475

It is simplified in version 2.0.0 (Google.Cloud.Firestore)

 FirestoreDb firestormDb = new FirestoreDbBuilder
                {
                    ProjectId = "project id",
                    JsonCredentials = "<JSON AS STRING>"
                }.Build();

Upvotes: 2

Jack Fox
Jack Fox

Reputation: 61

After much googling I've found that you can create a credential from Json and use this to create a FirestoreClient which you then pass to the FirestoreDb.

 var credential = GoogleCredential.FromJson(<JSON AS STRING>);
 var channelCredentials = credential.ToChannelCredentials();
 var channel = new Channel(FirestoreClient.DefaultEndpoint.ToString(), channelCredentials);
 var client = FirestoreClient.Create(channel);
 var firestoreDb = FirestoreDb.Create("project id", client);

Upvotes: 5

Related Questions