从Google身份验证服务检索.NET中REST服务的新Firebase访问令牌

mjp*_*lak 11 c# rest google-api firebase google-api-dotnet-client

更改了firebase授权系统后,我正在尝试从google auth服务器中检索c#中的访问令牌.

根据新文档:https://firebase.google.com/docs/reference/rest/database/user-auth#section-api-usage

我在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();
}
Run Code Online (Sandbox Code Playgroud)

gckey.json是从Google Developer Console下载的特定firebase项目的密钥文件.

代码工作正常,但它返回不与firebase一起使用的令牌,我试过: https://fiery-torch-xxxx.firebaseio.com/.json?access_token=retrived token

但我收到: "error" : "Permission denied."

我究竟做错了什么?或者我错过了什么?

小智 5

我在示波器中加入" https://www.googleapis.com/auth/userinfo.email " 之后就开始工作了

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();
}
Run Code Online (Sandbox Code Playgroud)

我在阅读以下谷歌小组帖子时发现了这一点:

使用Google Oauth2访问令牌时,权限被拒绝错误