从Firebase数据库加载需要身份验证的数据抛出错误firebase无法解析身份验证令牌

时间:2017-06-28 14:03:34

标签: 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 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无法解析身份验证令牌

这是指向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.");
}

我的实施在哪里出错了?

1 个答案:

答案 0 :(得分:1)

我希望得到更新,因为我终于找到了解决方案,对我来说......

以下是我从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;
}