Loading data from a Firebase database that requires authentication, a firebase error cannot parse an authentication token

I have a website that uses SQL Server on Azure for all of its data. I am working with another company to get additional information for specific records that exist in my database.

When these "specific records" are viewed, I want to provide another link to get this additional information from the Firebase database.

So, I'm trying to write a service that will retrieve this data, here is what I wrote before 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;
}

This is the error I return from Firebase when I try to make a call var stats = await firebase.Child()....:

firebase could not parse authentication 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.");
}

?

+4
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;
}
+1

Source: https://habr.com/ru/post/1680323/


All Articles