从需要身份验证的 Firebase 数据库加载数据会抛出错误 firebase 无法解析身份验证令牌
Loading data from Firebase database which requires authentication throws error firebase could not parse auth token
我有一个网站,它利用 SQL Azure 上的服务器来获取所有数据。我正在与另一家公司合作,为我的数据库中存在的特定记录获取额外的补充信息。
当那些 "specific reccords" 被查看时,我想提供另一个 link 以从 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()....
调用时从 Firebase 返回的错误:
firebase could not parse auth token
link FirebaseDatabase.NET Github 页面:https://github.com/step-up-labs/firebase-database-dotnet
以防 link 将来死掉,这里是我正在尝试复制的确切示例,该示例显示在该站点上:
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;
}
我有一个网站,它利用 SQL Azure 上的服务器来获取所有数据。我正在与另一家公司合作,为我的数据库中存在的特定记录获取额外的补充信息。
当那些 "specific reccords" 被查看时,我想提供另一个 link 以从 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()....
调用时从 Firebase 返回的错误:
firebase could not parse auth token
link FirebaseDatabase.NET Github 页面:https://github.com/step-up-labs/firebase-database-dotnet
以防 link 将来死掉,这里是我正在尝试复制的确切示例,该示例显示在该站点上:
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;
}