如何使用 C# 获取身份验证令牌以获取 VSO 工作项

How to get Authentication Token to get VSO Workitems using C#

我需要使用 C# Rest API 调用获取 VSO workitem。 我无法弄清楚如何为我的 http 请求获取令牌。 以下是我所拥有的。谁能给我代码让令牌验证到VSO。

https://docs.microsoft.com/en-us/rest/api/azure/devops/wit/work%20items/get%20work%20item?view=azure-devops-rest-6.0

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace GetVSOTask
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string token = "?????";           
            var httpClient = new HttpClient
            {
                BaseAddress = new Uri("https://dev.azure.com/")
            };
            string URI = $"Microsoft/OSGS/_apis/wit/workitems/31054512?&api-version=6.0";

            httpClient.DefaultRequestHeaders.Remove("Authorization");
            httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            HttpResponseMessage response = await httpClient.GetAsync(URI).ConfigureAwait(false);
            var HttpsResponse = await response.Content.ReadAsStringAsync();

            Console.WriteLine(HttpsResponse);

            Console.ReadLine();
        }
    }
}

您需要在 Azure DevOps 门户中生成令牌:

  1. 在 Azure DevOps 中登录您的组织 (https://dev.azure.com/{yourorganization})

  2. 从您的主页,打开您的用户设置,然后 select 个人访问令牌。

  3. 然后 select + 新令牌。

  4. 为您的令牌命名,select您要使用令牌的组织,然后为您的令牌选择生命周期。

  5. Select 此令牌授权您执行特定任务的范围。

例如,要创建令牌以使构建和发布代理能够向 Azure DevOps Services 进行身份验证,请将令牌的范围限制为代理池(阅读& 管理)。要读取审计日志事件,以及管理和删除流,select 读取审计日志,然后 select 创建。

  1. 完成后,请务必复制令牌。为了您的安全,它不会再次显示。使用此令牌作为您的密码。

创建 PAT 后,您可以在 Azure DevOps 中需要用户凭据进行身份验证的任何地方使用它。

查看 here 更多信息。

要将 PAT 令牌传递给 C# HTTP header,您需要将其转换为 Base64 字符串。

string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", "PAT")));

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

这是一个例子:

       ...

        {
            string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", "PAT")));
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri($"https://dev.azure.com/{OrganizationName}");  //url of your organization
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);

                //connect to the REST endpoint            
                HttpResponseMessage response = client.GetAsync("/ProjectName/_apis/wit/workitems/467?api-version=6.0").Result;

                //check to see if we have a successful response
                if (response.IsSuccessStatusCode)
                {
                    var value = response.Content.ReadAsStringAsync().Result;
                    Console.WriteLine(value);
                    Console.ReadLine();
                }


            }
        }

更详细的信息可以参考this doc or Get started with the REST APIs