Google-cloud-dotnet: [Firestore] Manual auth

Created on 18 Jul 2018  路  13Comments  路  Source: googleapis/google-cloud-dotnet

Hello, how can i provide the service account thru code? FirestoreDb.Create does not have an overload for the path to the key file

firestore question

Most helpful comment

Thanks Jon, i managed to get it working with your instructions, this is the code i ended up with:
c# using Google.Cloud.Firestore; using Google.Apis.Auth.OAuth2; using Google.Cloud.Firestore.V1Beta1; using Grpc.Core; using Grpc.Auth; ... GoogleCredential credential = GoogleCredential .FromFile("auth.json"); ChannelCredentials channelCredentials = credential.ToChannelCredentials(); Channel channel = new Channel(FirestoreClient.DefaultEndpoint.ToString(), channelCredentials); FirestoreClient firestoreClient = FirestoreClient.Create(channel); FirestoreDb firestoreDb = FirestoreDb.Create("doc-location", client: firestoreClient);

All 13 comments

You need to do this in two stages:

  • Create a FirestoreClient that uses the auth you want
  • Pass that into FirestoreDb.Create

It's not as simple to create a FirestoreClient with custom auth as we'd like it to be, but the process described in the FAQ (just use FirestoreClient instead of PublisherClient). The steps are:

  • Create a GoogleCredential however you want to, e.g. GoogleCredential.FromFile
  • Create a ChannelCredentials from that using the ToChannelCredentials extension method (make sure you have a using directive for the Grpc.Auth namespace)
  • Create a Channel with those credentials to the default endpoint (FirestoreClient.DefaultEndpoint)
  • Use FirestoreClient.Create(Channel)

If you don't need to shut the channel down (which you usually don't) you can then forget the channel in your code. Otherwise, you'll need to keep a reference to it for shutdown - you can't obtain the channel from the FirestoreClient

It's likely that creating a FirestoreDb with custom auth will continue to need the FirestoreClient to be created first, but we really really want to make it simpler to do that. We have plans to create a new auth library, at which point we can add a FirestoreClient.Create(SomeNewAuthInterface) method - but that library isn't in place yet.

Please add a comment to this issue when you've tried these steps, so I can close it or help you further.

Thanks Jon, i managed to get it working with your instructions, this is the code i ended up with:
c# using Google.Cloud.Firestore; using Google.Apis.Auth.OAuth2; using Google.Cloud.Firestore.V1Beta1; using Grpc.Core; using Grpc.Auth; ... GoogleCredential credential = GoogleCredential .FromFile("auth.json"); ChannelCredentials channelCredentials = credential.ToChannelCredentials(); Channel channel = new Channel(FirestoreClient.DefaultEndpoint.ToString(), channelCredentials); FirestoreClient firestoreClient = FirestoreClient.Create(channel); FirestoreDb firestoreDb = FirestoreDb.Create("doc-location", client: firestoreClient);

Great - will close the issue. So glad it worked :)

Hello, any guidance in my issue? I've been searching feverishly for a resolution and haven't come across one yet.
I'm using Visual Studio For Mac, developing for xamarin android

Problem *
I am not able to successfully authenticate and create Channel using the recommendation above. I am able to successfully get my credential, but line 58 - *
creating channel
it crashes.

screen shot 2018-11-14 at 11 07 25 pm

@joshuabooker: Xamarin isn't a supported platform, I'm afraid. Our libraries depend on the gRPC library, which contains a native library. While it's technically feasible for that to be cross-compiled for all the relevant platforms, and packaged up appropriately, we have higher priority work items at the moment. We'd like to support a broader range of platforms, but I don't expect that to happen any time soon.

@joshuabooker This isn't produced or supported by us, but might this package allow you to use firestore on xamarin? https://www.nuget.org/packages/Xamarin.Firebase.Firestore/

Thanks Jon, i managed to get it working with your instructions, this is the code i ended up with:

using Google.Cloud.Firestore;
using Google.Apis.Auth.OAuth2;
using Google.Cloud.Firestore.V1Beta1;
using Grpc.Core;
using Grpc.Auth;
...
GoogleCredential credential = GoogleCredential
            .FromFile("auth.json");
ChannelCredentials channelCredentials = credential.ToChannelCredentials();
Channel channel = new Channel(FirestoreClient.DefaultEndpoint.ToString(), channelCredentials);
FirestoreClient firestoreClient = FirestoreClient.Create(channel);
FirestoreDb firestoreDb = FirestoreDb.Create("doc-location", client: firestoreClient);

you麓re the man!... Thanks so much bro!

Hello, the solution was supurb & worked several day. But now I am getting following error. I didn't make any changed but don't know what is happening. I am using this in a wpf application.
Error details:

Status(StatusCode=Unauthenticated, Detail="Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.")

@bluetoothfx: Please raise a new issue with more context and sample code. (Although I'm on vacation this week, so replies may take a bit longer.)

@jskeet Sorry for my late response. I was taking time because before I post I was making sure on my end if anything I am missing. And I found that I was switching between Ubuntu and Windows and I don't know why my Windows System date time was not correct. And when that was incorrect I was getting Status(StatusCode=Unauthenticated error. After adjusting the time, It was alright and worked as expected. Thanks. :-)

@bluetoothfx: Excellent, glad to hear it's okay.

How to do this in 2.0.0-beta* version? There is no parameter in FirestoreClient.Create().

@DenisNP: It's much easier now - just use FirestoreDbBuilder and set one of the appropriate properties there - just as documented here: https://googleapis.github.io/google-cloud-dotnet/docs/faq.html#how-can-i-use-non-default-credentials-for-grpc-based-apis

Was this page helpful?
0 / 5 - 0 ratings