question

smumair06 avatar image
smumair06 asked

Authenticate Firebase RealtimeDatabase via REST API

Hello guys, I have been trying to fetch data from firebase realtime database with my azure function (cloud script) and I managed to get that, if database has public read/write rules. But I want to limit access only after authentication. So, I decided to go with OAuth2 authentication.
I have created service account key file on firebase but, I have no idea how to use that in Azure function since firebase documentation don't provide C# sample for that. (https://firebase.google.com/docs/database/rest/auth#google_oauth2_access_tokens)

My Current code is:

            HttpRequestMessage httpRequestMessage = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(FIREBASE_URL),
                Headers =
                {
                    { HttpRequestHeader.Accept.ToString(), "application/json;odata=nometadata" },
                    { HttpRequestHeader.Authorization.ToString(), "Bearer ???" }
                }
            };

            HttpClient requestClient = new HttpClient();
            HttpResponseMessage response = await requestClient.SendAsync(httpRequestMessage);
            string rawData = await response.Content.ReadAsStringAsync();

This code runs without authentication, though.
But, I need to generate access token from my service account key file. Can anyone guide me how can I generate the token?

Thanks,

apisCloudScriptAuthentication
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Rick Chen avatar image
Rick Chen answered

This question is out of our scope, please contact the Google support for help. Here is a document I found that can be helpful: OAuth 2.0 | API Client Library for .NET | Google Developers.

The Azure Function also support Nodejs, in case you want to switch to Nodejs.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.