C# 从需要身份验证的Firebase数据库加载数据时引发错误Firebase无法解析身份验证令牌

C# 从需要身份验证的Firebase数据库加载数据时引发错误Firebase无法解析身份验证令牌,c#,firebase,asp.net-core,firebase-realtime-database,asp.net-core-1.1,C#,Firebase,Asp.net Core,Firebase Realtime Database,Asp.net Core 1.1,我有一个网站,利用Azure上的SQL Server获取所有数据。我正在与另一家公司合作,以获取数据库中存在的特定记录的附加补充信息 当查看这些“特定记录”时,我想提供另一个链接,从Firebase数据库检索补充信息 因此,我正在尝试编写一个服务来检索这些数据,以下是我编写的PoC: private readonly string firebaseUrl = "https://{myproject}.firebaseio.com/"; private readonly string authTo

我有一个网站,利用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 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.");
}
var firebase=新的FirebaseClient(“https://dinosaur-facts.firebaseio.com/");
var dinos=等待火力基地
.儿童(“恐龙”)
.OrderByKey()
.StartAt(“翼手龙”)
.LimitToFirst(2)
.OnceAsync();
foreach(以恐龙为单位的var恐龙)
{
WriteLine($“{dino.Key}是{dino.Object.Height}米高。”);
}

我的实现哪里出了问题

只是想在我最终找到解决方案时更新一下,对我来说

以下是我从中获得解决方案的原因:

此版本传递电子邮件地址和密码以获取身份验证令牌。一旦我们有了令牌,它就会像以前一样传递给Firebase客户端

(注意:我需要添加FirebaseAuthentication.net nuget包)

公共异步任务GetInMatchStats()
{
常量字符串firebaseUrl=”https://xxxxxxxxxxxxxx.firebaseio.com/";
常量字符串firebaseUsername=”xxxxxxxxxxxxxxxxxxxxx@xxxxx.xxx";
常量字符串firebasePassword=“xxxxxxxx”;
常量字符串firebaseApiKey=“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”;
bool-tryAgain=false;
FirebaseAuthLink令牌=空;
尝试
{
var auth=新的FirebaseAuthProvider(新的FirebaseConfig(firebaseApiKey));
做
{
尝试
{
token=使用EmailandPasswordAsync(firebaseUsername,firebasePassword)等待身份验证登录;
}
捕获(FirebaseAuthException faException)
{
//检查错误的tryAgain,因为我们不想尝试创建两次帐户
if(faException.Reason.ToString()=“UnknownEmailAddress”&&&!tryAgain)
{
//创建,然后登录
token=wait auth.CreateUserWithEmailAndPasswordAsync(firebaseUsername,firebasePassword,“Greg”,false);
tryAgain=true;
}
投掷;
}
捕获(例外)
{
投掷;
}
}
while(tryAgain);
var firebase=new FirebaseClient(firebaseUrl,new FirebaseOptions{AuthTokenAsyncFactory=()=>Task.FromResult(token.FirebaseToken)});
var stats=await firebase.Child(“my/collection/path”).OnceAsync();
如果(stats==null | |!stats.Any())
{
返回null;
}
//返回等待转换(stats.AsEnumerable());
}
捕获(异常)
{
var message=exception.ToString();
}
返回null;
}

您确定使用的应用程序密码正确吗?您从哪里获得authToken的?我在Github上看到一些文章和问题,这些文章和问题都提到使用了错误的令牌,这取决于它在控制台中的位置。
数据库链接和遗留秘密创建已经移动,现在位于项目设置中的“服务帐户”下。
在Firebase控制台的@Nkosi中找到,我转到了“齿轮/轮子”,=>项目设置,然后是名为“Web Api密钥”的属性。看起来我第一次尝试使用同一页面上的“应用程序Id”。最后,我尝试转到“cog/wheel”=>用户和权限(到IAM&Admin页面)=>服务帐户=>“appengine默认服务帐户”。尝试了所有3个版本,它们都抛出了相同的错误
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.");
}
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;
}