使用 Firestore 对 c# 桌面应用程序进行身份验证

Prz*_*emo 4 c# desktop-application google-cloud-firestore

我准备了使用 firestore 数据库的桌面应用程序,为了访问它,我使用了 GOOGLE_APPLICATION_CREDENTIALS。根据文档,我无法为此类身份验证引入规则,但我想这样做。我已向支持人员发送了一个问题,并被告知使用 REST API。这是我的问题?是否可以通过 REST API 进行身份验证但读取和写入数据仍然使用 Google.Cloud.Firestore nuget ?这该怎么做 ?请记住,我有用 c# WPF 编写的桌面应用程序。也许你知道我的问题的其他解决方案?

Prz*_*emo 9

感谢您的反馈弗兰克,在您的链接中,我找到了答案。要从桌面应用程序对 Firestore 进行身份验证,您需要执行以下操作:

public FirestoreDb CreateFirestoreDbWithEmailAuthentication(string emailAddress, 
string password, string firebaseApiKey, string firebaseProjectId)
{
            // Create a custom authentication mechanism for Email/Password authentication
            // If the authentication is successful, we will get back the current authentication token and the refresh token
            // The authentication expires every hour, so we need to use the obtained refresh token to obtain a new authentication token as the previous one expires
            var authProvider = new FirebaseAuthProvider(new FirebaseConfig(firebaseApiKey));
            var auth = authProvider.SignInWithEmailAndPasswordAsync(emailAddress, password).Result;
            var callCredentials = CallCredentials.FromInterceptor(async (context, metadata) =>
            {
                if (auth.IsExpired()) auth = await auth.GetFreshAuthAsync();
                if (string.IsNullOrEmpty(auth.FirebaseToken)) return;

                metadata.Clear();
                metadata.Add("authorization", $"Bearer {auth.FirebaseToken}");
            });
            var credentials = ChannelCredentials.Create(new SslCredentials(), callCredentials);

            // Create a custom Firestore Client using custom credentials
            var grpcChannel = new Channel("firestore.googleapis.com", credentials);
            var grcpClient = new Firestore.FirestoreClient(grpcChannel);
            var firestoreClient = new FirestoreClientImpl(grcpClient, FirestoreSettings.GetDefault());

            return FirestoreDb.Create(firebaseProjectId, null, firestoreClient);
}
Run Code Online (Sandbox Code Playgroud)

当然,首先您需要在 Firestore 控制台中添加电子邮件用户。


Fra*_*len 0

从查看的 Github 存储库google-cloud-dotnet来看,它们似乎总是用来GOOGLE_APPLICATION_CREDENTIALS访问 Firestore。

在这种情况下,无法像 Firebase 身份验证凭据一样使用该库。回购协议上的这些问题似乎也指向了这个方向。

在这种情况下,要使用 Firebase 身份验证凭据访问 Firestore,您必须通过 REST API 访问它。