Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 4807

Firebase Realtime Database REST API returns Unauthorized, even with token

$
0
0

I'm writting a Blazor Server app that connects to Firebase Realtime Database. I'm using the FirebaseDatabase SDK but need to perform a set of Atomic operations updating multiple Paths. Since the SDK do not have a method for that, I've decided to use REST API.

I'm already able to Authenticate using email/pass, which gives me access to currentUser.GetIdTokenAsync();

I'm able to read/write data on single Put/Patch operations.

But, when I try to update a path using the REST API, it fails saying: "Unauthorized".

In order to test this, I've written a simple code, below:

public async Task singleUpdate(string idPropriedade, string idLote, string novoNome){    // Firebase database URL    string firebaseUrl = DatabaseProjectSettings.projectURL;    // Define the updates for each path    string jsonContent = "{" +"\"lotes\": {" +"\"" + idPropriedade +"\": {" +"\"" + idLote +"\": {" +"\"nomePropriedade\": \"" + novoNome +"\"" +"}" +"}" +"}" +"}";    var token = await _currUser.GetIdTokenAsync(true);    // Create HttpClient instance    using (HttpClient client = new HttpClient())    {        // Configure the request        HttpRequestMessage request = new HttpRequestMessage        {            Method = HttpMethod.Put,            RequestUri = new Uri($"{firebaseUrl}.json"),            Content = new StringContent(jsonContent, System.Text.Encoding.UTF8, "application/json")        };        request.Headers.Add("Authorization", $"Bearer {token}");        // Send the request and get the response        HttpResponseMessage response = await client.SendAsync(request);        // Check if the request was successful        if (response.IsSuccessStatusCode)        {            Console.WriteLine("Paths updated successfully.");        }        else        {            Console.WriteLine($"Failed to update paths. Status code: {response.StatusCode}");        }    }}

If I remove the database rules, the write operation works fine. If I leave the database rules, I get the "Unauthorized" message.

I have already tested to use the token on the URL as follow, using both "auth" or "access_token": RequestUri = new Uri($"{firebaseUrl}.json?access_token={token}"), without success!

This is the response in VStudio:

enter image description here

Please send me your insights! Thanks!


Viewing all articles
Browse latest Browse all 4807

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>