12

After a change of firebase authorization system, I'm trying to retrieve access token in c# from google auth server.

According to new documentation: https://firebase.google.com/docs/reference/rest/database/user-auth#section-api-usage

I created something similar in c#:

using Google.Apis.Auth.OAuth2;
[...]
async Task<string> GetToken()
{
    GoogleCredential credential;
    using (var stream = new System.IO.FileStream("gckey.json",
        System.IO.FileMode.Open, System.IO.FileAccess.Read))
    {
        credential = GoogleCredential.FromStream(stream).CreateScoped(
            new string[] { "https://www.googleapis.com/auth/firebase.database" }
            );
    }

    ITokenAccess c = credential as ITokenAccess;
    return await c.GetAccessTokenForRequestAsync();
}

gckey.json is key file downloaded from Google Developer console for specific firebase project.

Code works fine, but it returns token that is not working with firebase, I have tried: https://fiery-torch-xxxx.firebaseio.com/.json?access_token=retrived token

but I receive: "error" : "Permission denied."

What am I doing wrong? Or what I'm missing?

Cœur
  • 37,241
  • 25
  • 195
  • 267
mjpolak
  • 721
  • 6
  • 24

2 Answers2

7

I got this to work after I included "https://www.googleapis.com/auth/userinfo.email" in the scopes

using Google.Apis.Auth.OAuth2;
[...]
async Task<string> GetToken()
{
    GoogleCredential credential;
    using (var stream = new System.IO.FileStream("gckey.json",
        System.IO.FileMode.Open, System.IO.FileAccess.Read))
    {
        credential = GoogleCredential.FromStream(stream).CreateScoped(
            new string[] { 
                "https://www.googleapis.com/auth/firebase.database", 
                "https://www.googleapis.com/auth/userinfo.email" }
            );
    }

    ITokenAccess c = credential as ITokenAccess;
    return await c.GetAccessTokenForRequestAsync();
}

I discovered this while reading the following google group thread:

Permission denied error when using Google Oauth2 access token

  • This is what I'm going through now. Do you have to get a new token before every call to firebase? Or is this code run only once? – JAck28 Nov 07 '18 at 14:54
1

I read the docs and it states the the url parameter should be "auth" not "access_token". Can you try that?

XPscode
  • 87
  • 1
  • 4
Tomáš Bezouška
  • 1,395
  • 1
  • 10
  • 25
  • Unfortunately not, response is: "Could not parse auth token.". I believe that auth is relic after old authorizing system, described in old documentation: https://www.firebase.com/docs/rest/api/ – mjpolak May 24 '16 at 06:05
  • Did you try firebase support? – Tomáš Bezouška Jun 01 '16 at 16:15
  • Firebase support responses are a little bit slow currently. I believe it is because of fusion with google. – mjpolak Jun 27 '16 at 06:12