我有一个网站,它利用Azure上的SQL Server来获取所有数据.我正在与另一家公司合作,为我的数据库中存在的特定记录获取额外的补充信息.
当正在查看这些“特定记录”时,我想提供另一个链接以从Firebase数据库中检索该补充信息.
所以,我正在尝试编写一个可以检索这些数据的服务,这就是我迄今为止所写的PoC:
private readonly string firebaseUrl = "https://{myproject}.firebaseio.com/";
private readonly string authToken = "x:xxxxxxxxxxx:xxx:xxxxxxxxxxxxxxxx";
public async Task<IEnumerable<InMatchStat>> GetInMatchStats()
{
try
{
var firebase = new FirebaseClient(firebaseUrl, new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(authToken) });
var stats = await firebase.Child("my/collection/path").OnceAsync<InMatchStat>();
if (stats == null || !stats.Any())
{
return null;
}
return await Convert(stats.AsEnumerable());
}
catch (Exception exception)
{
var message = exception.ToString();
}
return null;
}
这是我在尝试执行var stats = await firebase.Child()…. call时从Firebase返回的错误:
firebase could not parse auth token
这是FirebaseDatabase.NET Github页面的链接:https://github.com/step-up-labs/firebase-database-dotnet
如果链接在将来失效,这里是我正在尝试复制的确切示例,该示例显示在该站点上:
var auth = "ABCDE"; // your app secret
var firebaseClient = new FirebaseClient(
"<URL>",
new FirebaseOptions
{
AuthTokenAsyncFactory = () => Task.FromResult(auth)
});
以及该页面的查询示例:
var firebase = new FirebaseClient("https://dinosaur-facts.firebaseio.com/");
var dinos = await firebase
.Child("dinosaurs")
.OrderByKey()
.StartAt("pterodactyl")
.LimitToFirst(2)
.OnceAsync<Dinosaur>();
foreach (var dino in dinos)
{
Console.WriteLine($"{dino.Key} is {dino.Object.Height}m high.");
}
我的实施在哪里出错了?
最佳答案 只是想让这个更新,因为我终于想出了解决方案,对我来说……
这是我从中获得解决方案的地方:https://github.com/step-up-labs/firebase-database-dotnet/issues/60
此版本传递电子邮件地址和密码以获取身份验证令牌.然后,一旦我们拥有令牌,就会像以前一样将其传递给Firebase客户端.
(注意:我需要添加FirebaseAuthentication.net nuget包)
public async Task<IEnumerable<InMatchStat>> GetInMatchStats()
{
const string firebaseUrl = "https://xxxxxxxxxxxxxx.firebaseio.com/";
const string firebaseUsername = "xxxxxxxxxxxxxxxxxxxxx@xxxxx.xxx";
const string firebasePassword = "xxxxxxxx";
const string firebaseApiKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
bool tryAgain = false;
FirebaseAuthLink token = null;
try
{
var auth = new FirebaseAuthProvider(new FirebaseConfig(firebaseApiKey));
do
{
try
{
token = await auth.SignInWithEmailAndPasswordAsync(firebaseUsername, firebasePassword);
}
catch (FirebaseAuthException faException)
{
// checking for a false tryAgain because we don't want to try and create the account twice
if (faException.Reason.ToString() == "UnknownEmailAddress" && !tryAgain)
{
// create, then signin
token = await auth.CreateUserWithEmailAndPasswordAsync(firebaseUsername, firebasePassword, "Greg", false);
tryAgain = true;
}
throw;
}
catch (Exception)
{
throw;
}
}
while (tryAgain);
var firebase = new FirebaseClient(firebaseUrl, new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(token.FirebaseToken) });
var stats = await firebase.Child("my/collection/path").OnceAsync<InMatchStat>();
if (stats == null || !stats.Any())
{
return null;
}
//return await Convert(stats.AsEnumerable());
}
catch (Exception exception)
{
var message = exception.ToString();
}
return null;
}