代码之家  ›  专栏  ›  技术社区  ›  Vinny

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

  •  1
  • Vinny  · 技术社区  · 5 年前

    工作项 使用 C#Rest API 我不知道如何获取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();
            }
        }
    }
    
    0 回复  |  直到 5 年前
        1
  •  0
  •   Kevin Lu-MSFT    5 年前

    要将PAT令牌传递给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 doc Get started with the REST APIs

        2
  •  0
  •   Shayki Abramczyk Cece Dong - MSFT    5 年前

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

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

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

    3. 然后选择+New Token。

    4. 命名令牌,选择要使用令牌的组织,然后选择令牌的寿命。

    5. 选择此令牌的作用域以授权执行特定任务。

    ,要创建令牌以使生成和发布代理能够对Azure DevOps服务进行身份验证,请将令牌的作用域限制为代理池(读取和管理)。要读取审核日志事件并管理和删除流,请选择“读取审核日志”,然后选择“创建”。

    1. 完成后,请确保复制令牌。为了你的安全,它不会再出现了。使用此令牌作为密码。

    一旦创建了PAT,您就可以在任何需要您的用户凭据才能在azuredevops中进行身份验证的地方使用它。

    看到了吗 here 更多信息。