如何使用.NET进行身份验证和使用Sonarcloud API

问题描述 投票:0回答:1

我需要使用几个 SonarCloud API,但大多数都需要身份验证。如何使用令牌或类似的东西从 .net 代码进行身份验证和消费?

感谢您的反馈

我有一个需要使用的 API 列表,一旦我登录到 SonarCloud,我就可以从网络上使用它们并查看信息,但是我需要执行相同的操作,但使用 .net 代码来组织信息和将其保存在数据库中

.net sonarcloud
1个回答
0
投票

我终于使用以下代码从.net连接到声纳“声纳使用不记名令牌身份验证”

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());
}
© www.soinside.com 2019 - 2024. All rights reserved.