Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to Cloud Firestore DB with .net core?

So far all the examples of using Google Cloud Firestore with .net show that you connect to your Firestore db by using this command:

FirestoreDb db = FirestoreDb.Create(projectId);

But is this skipping the step of authentication? I can't seem to find an example of wiring it up to use a Google service account. I'm guessing you need to connect using the service account's private_key/private_key_id/client_email?

like image 206
mkimmet Avatar asked Jul 19 '18 11:07

mkimmet


People also ask

How do I find my firestore database URL?

You can find your Realtime Database URL in the Realtime Database section of the Firebase console. Depending on the location of the database, the database URL will be in one of the following forms: https:// DATABASE_NAME . firebaseio.com (for databases in us-central1 )


4 Answers

This worked for me.

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");
like image 165
Pieter Linde Avatar answered Oct 01 '22 17:10

Pieter Linde


I could not compile @Michael Bleterman's code, however the following worked for me:

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());

Packages I use:

<PackageReference Include="Google.Cloud.Firestore" Version="2.0.0-beta02" />
<PackageReference Include="Google.Cloud.Storage.V1" Version="2.5.0" />
like image 43
Łukasz Sypniewski Avatar answered Oct 23 '22 08:10

Łukasz Sypniewski


But is this skipping the step of authentication?

No. It will use the default application credentials. If you're running on Google Cloud Platform (AppEngine, GCE or GKE), they will just be the default service account credentials for the instance. Otherwise, you should set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to a service account credential file.

From the home page of the user guide you referred to:

When running on Google Cloud Platform, no action needs to be taken to authenticate.

Otherwise, the simplest way of authenticating your API calls is to download a service account JSON file then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to it. The credentials will automatically be used to authenticate. See the Getting Started With Authentication guide for more details.

It's somewhat more awkward to use non-default credentials; this recent issue gives an example.

like image 6
Jon Skeet Avatar answered Oct 23 '22 08:10

Jon Skeet


You can also use the credentials stored in a json file:

    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);
like image 13
Michael Bleterman Avatar answered Oct 23 '22 09:10

Michael Bleterman