最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Firebase distribution api failed OAuth2 authorization - Stack Overflow

programmeradmin0浏览0评论

I need help understanding why I can't get info from distribution api.

I already check that I have the api enabled in the console, I double checked the service account to make sure its correct and everything seems ok

var credential = GoogleCredential.FromJsonParameters(new JsonCredentialParameters()
{
    Type = "service_account",
    ProjectId = "myApp",
    PrivateKeyId = "************",
    PrivateKey =
        "-----BEGIN PRIVATE KEY-----key-----END PRIVATE KEY-----\n",
    ClientEmail = "firebase-adminsdk-fbsvc@*******.iam.gserviceaccount",
    ClientId = "**************",
    TokenUri = ";,
    UniverseDomain = "googleapis"
})


var token = await Credential.UnderlyingCredential.GetAccessTokenForRequestAsync(";);


try
{
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        string url = ";;
        HttpResponseMessage response = await client.GetAsync(url);
        string result = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Response: " + result);
    }
}
catch(Exception e)
{
    
}

I'm trying to access firebase distribution api to see the latest release I have but the request is always throwing a 401 saying

Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential

I don't really know what im doing wrong

I need help understanding why I can't get info from distribution api.

I already check that I have the api enabled in the console, I double checked the service account to make sure its correct and everything seems ok

var credential = GoogleCredential.FromJsonParameters(new JsonCredentialParameters()
{
    Type = "service_account",
    ProjectId = "myApp",
    PrivateKeyId = "************",
    PrivateKey =
        "-----BEGIN PRIVATE KEY-----key-----END PRIVATE KEY-----\n",
    ClientEmail = "firebase-adminsdk-fbsvc@*******.iam.gserviceaccount",
    ClientId = "**************",
    TokenUri = "https://oauth2.googleapis/token",
    UniverseDomain = "googleapis"
})


var token = await Credential.UnderlyingCredential.GetAccessTokenForRequestAsync("https://accounts.google/o/oauth2/auth");


try
{
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        string url = "https://firebaseappdistribution.googleapis/v1/projects/myApp/apps/myApp/releases";
        HttpResponseMessage response = await client.GetAsync(url);
        string result = await response.Content.ReadAsStringAsync();
        Console.WriteLine("Response: " + result);
    }
}
catch(Exception e)
{
    
}

I'm trying to access firebase distribution api to see the latest release I have but the request is always throwing a 401 saying

Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential

I don't really know what im doing wrong

Share Improve this question edited Mar 2 at 15:31 Frank van Puffelen 601k85 gold badges890 silver badges860 bronze badges Recognized by Google Cloud Collective asked Mar 2 at 13:59 DOCZICDOCZIC 93 bronze badges 1
  • You are using OAUTH2 which is a two step authentication. Step 1 is to get a cookie/token from server. Step 2 is to use token to make a query. You are failing step 1. The token return is no good. So you are failing step 1(error 401 - not authorized) The following may help : learn.microsoft/en-us/entra/identity-platform/… – jdweng Commented Mar 2 at 14:23
Add a comment  | 

1 Answer 1

Reset to default 0

It looks like your issue is caused by using an incorrect OAuth scope when requesting an access token. Firebase APIs typically require one of the following scopes:

Cloud Platform Scope (recommended for broader Firebase access):

var token = await var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync("https://www.googleapis/auth/cloud-platform");
services):

Firebase Scope (if you want to limit access specifically to Firebase-related services):

var token = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync("https://www.googleapis/auth/firebase");

Also verify that your service account has the necessary IAM role (Firebase App Distribution Admin).

发布评论

评论列表(0)

  1. 暂无评论