Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Firestore] Manual auth #2349

Closed
chalber opened this issue Jul 18, 2018 · 15 comments
Closed

[Firestore] Manual auth #2349

chalber opened this issue Jul 18, 2018 · 15 comments
Assignees
Labels
api: firestore Issues related to the Firestore API. type: question Request for information or clarification. Not an issue.

Comments

@chalber
Copy link

chalber commented Jul 18, 2018

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

@jskeet
Copy link
Collaborator

jskeet commented Jul 18, 2018

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.

@jskeet jskeet assigned chrisdunelm and jskeet and unassigned chrisdunelm Jul 18, 2018
@jskeet jskeet added the type: question Request for information or clarification. Not an issue. label Jul 18, 2018
@chalber
Copy link
Author

chalber commented Jul 18, 2018

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

@jskeet
Copy link
Collaborator

jskeet commented Jul 18, 2018

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

@jskeet jskeet closed this as completed Jul 18, 2018
@joshuabooker
Copy link

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

@jskeet
Copy link
Collaborator

jskeet commented Nov 15, 2018

@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.

@chrisdunelm
Copy link
Contributor

@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/

@jskeet jskeet added the api: firestore Issues related to the Firestore API. label Apr 17, 2019
@edycarreyes99
Copy link

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!

@bluetoothfx
Copy link

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.")

@jskeet
Copy link
Collaborator

jskeet commented May 27, 2019

@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.)

@bluetoothfx
Copy link

bluetoothfx commented Jun 2, 2019

@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. :-)

@jskeet
Copy link
Collaborator

jskeet commented Jun 2, 2019

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

@DenisNP
Copy link

DenisNP commented Apr 25, 2020

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

@jskeet
Copy link
Collaborator

jskeet commented Apr 25, 2020

@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

@mohumohu-corp
Copy link

mohumohu-corp commented Dec 7, 2021

As of 2021, the above link does not seem to work.
It worked for me by writing this.

var firestoreDbBuilder = new FirestoreDbBuilder {
            ProjectId = "your firebase project id", 
            ChannelCredentials = GoogleCredential.FromFile("path to your credential json file.").ToChannelCredentials()// or FromFileAsync()
        };
var firestoreDb = firestoreDbBuilder.Build(); // or BuildAsync()

@jskeet
Copy link
Collaborator

jskeet commented Dec 7, 2021

@mohumohu-corp: Sorry for that link no longer working... but there are now details here: https://cloud.google.com/dotnet/docs/reference/help/getting-started

In this case, the simpler code would be:

var firestoreDbBuilder = new FirestoreDbBuilder
{
    ProjectId = "your firebase project id",
    CredentialsPath = "path to your credential json file."
}.Build();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

8 participants