step-up-labs / firebase-database-dotnet

C# library for Firebase Realtime Database.
MIT License
671 stars 171 forks source link

"error" : "Could not parse auth token. #96

Closed felipeoliveirar closed 4 years ago

felipeoliveirar commented 7 years ago

Hi. can you help me with this issue. When I send the token in firebase option happens this error.

jaybowman commented 7 years ago

I'm using version 2.0.2 and firebaseAuthentication.net 3.0.5, here is how my code looks.

var token = _auth.FirebaseToken;

var query = await _client.Child(_userCollection) .Child(userId) .WithAuth(() => { return token; }) .OnceSingleAsync(); Sorry don't think this will help since I'm not on the current version that uses the new firebaseclient with the firebaseOptions object.

felipeoliveirar commented 7 years ago

my _client doesn't have the .WithAuth()

jaybowman commented 7 years ago

Yes, I had the same issue with version 3.0.2 so I reverted back to 2.0.2 which was working for me. Sorry I could not help.

felipeoliveirar commented 7 years ago

no problem. Thanks for help me.. :D

jaybowman commented 7 years ago

I have it working with the new 3.0.2 version. At first I got an error. Then I recompiled the firebase.dll from the src folder that is available from the nuget package. Then everything works. I guess the firebase.database package uses an auth token on all db calls, even if the Firebase db rules don't require it.

felipeoliveirar commented 7 years ago

How you did it? Can you print and post here? the step by step. After you did it you change all to .WithAuth() ?

jaybowman commented 7 years ago

With Version 3.0.2 there is not a public .WithAuth() method. Here is my initialization code.

var config = new FirebaseConfig("my firebase api key"); var authProvider = new FirebaseAuthProvider(config);

string user = System.Configuration.ConfigurationManager.AppSettings["firebaseUser"]; string pw = System.Configuration.ConfigurationManager.AppSettings["firebasePw"];

 _auth = await authProvider.SignInWithEmailAndPasswordAsync(user, pw);

  _fbClient = new FirebaseClient(_basePath,
            new FirebaseOptions()
            {
                AuthTokenAsyncFactory = () => Task.FromResult(_auth.FirebaseToken)
            });

Then in my getUser() (the users firebase collection has an authenticated user rule to retrieve users.)

var query = await _client.Child(_userCollection) .Child(userId) // .WithAuth(() => { return token; }) // previous version needed the withAuth() .OnceSingleAsync();

return query;

felipeoliveirar commented 7 years ago

hum.. Thanks @jaybowman I will try it here.. thanks

felipeoliveirar commented 7 years ago

@jaybowman I didn't find the FirebaseConfig are you using the FirebaseDatabase.net ? or the FirebaseAuth of Xamarin?

felipeoliveirar commented 7 years ago

I am making my connection this way:

      if (_client == null)
        {
            _client = new FirebaseClient(
                baseUrl: Constantes.Constantes.STRING_URL,
                options: new FirebaseOptions
                {
                    OfflineDatabaseFactory = (t, s) => new OfflineDatabase(t, s),
                });

            databaseOffline = _client.Child("Cliente").AsRealtimeDatabase<Cliente>();
            databaseOffline.SyncExceptionThrown += (s, ex) => Console.WriteLine(ex.Exception);
        }

adn the request this way

var itemsonline = _client
                .Child("Cliente")
                .OnceAsync<Cliente>();

            var retornodoservidor = await itemsonline;

            var listitemsnoservidor = retornodoservidor.ToList();
jaybowman commented 7 years ago

I'm using: package id="FirebaseAuthentication.net" version="3.0.5" targetFramework="net452" package id="FirebaseDatabase.net" version="3.0.2" targetFramework="net452"

My app is a commandline console application, not a Xamarin mobile app.

felipeoliveirar commented 7 years ago

hum.. thanks @jaybowman .. I will try to do here..

felipeoliveirar commented 7 years ago

@jaybowman when I try to connect.. appear when I try to sigging. return an exeption null. where i get this "my firebase api key". on google.json? I took that one. but didn't work

jaybowman commented 7 years ago

For my console app I'm using the firebase apikey from firebase web config. The API key is a little different for each firebase app type (Android - google-services.json, IOS - GoogleServices-info.plist, and web).

felipeoliveirar commented 7 years ago

keeps return null :( i don't know how to make that works..

felipeoliveirar commented 7 years ago

@jaybowman I did a new project just with this method of login and password.. but ever when I try to auth with mail and password.. it returns null. in your api-key you just insert it or make some change? I.E: https://your-api-key ??

JWiersema commented 6 years ago

I have this same problem with 'the simplest solution' (could not parse auth token): var auth = "ABCDE"; // your app secret var firebaseClient = new FirebaseClient( "", new FirebaseOptions { AuthTokenAsyncFactory = () => Task.FromResult(auth) }); I use the WEB API Key in from firebase > Web API Key from an .net Azure Function.

When I use FirebaseAuthentication.net and setup anonymous access in Firebase and then the code works: var config = new FirebaseConfig(FirebaseSettings.authKey); var authProvider = new FirebaseAuthProvider(config);

        var auth = await authProvider.SignInAnonymouslyAsync();

        this.fbClient = new FirebaseClient(
          FirebaseSettings.endPoint,
          new FirebaseOptions
          {
              AuthTokenAsyncFactory = () => Task.FromResult(auth.FirebaseToken)
          });

Then I can write to the database. But what I want is a database that is readonly for users but write-enabled for my server side code by app only token. How do I achieve this? Different question: I created my collection by hand. Is it possible to create (if not exists) this from within the .Net code using this library?

VladimirRybalko commented 6 years ago

@bezysoftware Any updates? I have absolutely the same issue. The simplest example does not work at all? I use my Web ApiKey and the latest version from Nuget 3.3.3

alombard commented 5 years ago

Hi Folks - I'm having the same issue - do I need to do something special with the basic example? Is Firebase supposed to be set up with Anonymous access for this library to work?

Cheers.

Andrew

alombard commented 5 years ago

Update on this - you need to use the now legacy Firebase Database Secret to get this to work. Apparently Firebase has deprecated this method of authentication so I would not bank on this working long term. That said my code started to magically work when I used the key from the Database Secrets portion of the project config.

image

Hope this helps,

Andrew

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

Closing the issue due to inactivity. Feel free to re-open