.netsonarcloud

How to authenticate and consume Sonarcloud API with .NET


I need to consume several of the SonarCloud APIs, but most of them require authentication. How do I do that authentication and consumption from .net code using a token or something like that?

I appreciate your feedback

I have a list of the APIs that I need to consume, and once I log into SonarCloud, from the web I can consume them and see the information, however I need to do the same but with .net code to organize the information and save it in a DB


Solution

  • I finally got to connect to sonar from .net using the following code "sonar use Bearer token Authentication":

    string responseBody = string.Empty;
    var APIUri = "https://sonarcloud.io/";
    var VSTSToken = "SonarToken";
    
    // Obtener Listado de proyectos en Azure
    uri = "api/issues/search?componentKeys=myproject&impactSeverities=HIGH";
    try
    {
        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", VSTSToken);
            using (HttpResponseMessage response = await client.GetAsync(APIUri + uri))
            {
                response.EnsureSuccessStatusCode();
                responseBody = await response.Content.ReadAsStringAsync();
    
            }
        }
    }
    catch (Exception ex)
    {
        //_Logger.LogError(ex.Message);
        //Console.WriteLine(ex.ToString());
    }