【问题标题】:How to get Authentication Token to get VSO Workitems using C#如何使用 C# 获取身份验证令牌以获取 VSO 工作项
【发布时间】:2021-03-25 17:54:57
【问题描述】:

我需要使用 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();
        }
    }
}

【问题讨论】:

  • 我已经有了你给的链接。我需要代码来获取令牌。
  • 嗨@Vinny。这张票有更新吗?如果答案能给你一些帮助,请随时告诉我。只是提醒this

标签: c# visual-studio azure-devops azure-devops-rest-api


【解决方案1】:

要将 PAT Token 传递给 C# HTTP 标头,您需要将其转换为 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 docGet started with the REST APIs

【讨论】:

    【解决方案2】:

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

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

    2. 在您的主页上,打开您的用户设置,然后选择个人访问令牌。

    3. 然后选择 + 新令牌。

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

    5. 选择此令牌的范围以授权您的特定任务。

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

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

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

    更多信息请参见here

    【讨论】:

    • 我有一个 PAT 令牌。我试图在上面的代码中传递它。我没有得到任何有效的结果。 :( 我的 Http Rest API 调用需要修剪吗?
    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 2015-06-21
    • 1970-01-01
    • 2022-03-03
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多