firebase-database-dotnet: Post with Authentication Results in Error 400 (Bad Request)

I’ve got some pretty simple code which should create a new ClassRoom object and save it to Firebase under “classRooms”. If I change my authentication rules in the database to allow universal access and I remove the authentication part of the request, everything works fine. So I think the issue has something to do with authentication.

Here’s the code which makes the request:

String id = subjectId.Text;
String name = subjectName.Text;
String activity = this.activity.Text;

var classRoom = await firebase
            .Child("classRooms")
            .WithAuth(auth.FirebaseToken)
            .PostAsync<ClassRoom>(new ClassRoom(id, name, activity));

And here’s the ClassRoom class:

class ClassRoom
    {
        public String subjectId;
        public String subjectName;
        public String activity;

        public ClassRoom(String subjectId, String subjectName, String activity)
        {
            this.subjectId = subjectId;
            this.subjectName = subjectName;
            this.activity = activity;
        }
    }

Thanks so much for any time anyone puts in to this. Your efforts are much appreciated 👍

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16

Commits related to this issue

Most upvoted comments

Hi folks, sorry for taking so long to reply, I was on vacation. Anyway, the solution mentioned by @jaybowman will work, however that’s not the preferred way of doing it. I will actually obsolete the “WithAuth” method and remove it later completely.

The preferred way of doing auth is using the FirebaseClient’s constructor with FirebaseOptions like this:

            var firebaseClient = new FirebaseClient(
                "<URL>",
                new FirebaseOptions
                {
                    AuthTokenAsyncFactory = () => Task.FromResult("...") // can also be async call
                });

I will amend the docs for auth to reflect this.

Here is a workaround, generate the key outside the postAsync() .

string key = Firebase.Database.FirebaseKeyGenerator.Next();
   // insert this works, creates a correctly formated URL
   await _client.Child("homes")
                       .Child(key)
                       .WithAuth(token)                              
                       .PostAsync<Home>(myhome, false);