我已经准备好了使用 firestore 数据库的桌面应用程序,为了访问它,我使用了 GOOGLE_APPLICATION_CREDENTIALS。根据文档,我无法引入此类身份验证的规则,但我想这样做。我已向支持人员发送问题,并被告知使用 REST API。这是我的问题?是否可以通过 REST API 进行身份验证,但读取和写入数据仍使用 Google.Cloud.Firestore nuget ?这个怎么做 ?请记住,我有用 c# WPF 编写的桌面应用程序。也许您知道我的问题的其他解决方案?
感谢您的反馈弗兰克,在您的链接中我找到了答案。要从桌面应用程序对 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);
}
当然,首先您需要在 firestore 控制台中添加电子邮件用户。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)