【问题标题】:Azure Devops Personal Access Token (PAT) stopped workingAzure Devops 个人访问令牌 (PAT) 停止工作
【发布时间】:2019-04-11 06:22:36
【问题描述】:

我能够很好地使用 Azure Devops RestAPI,但突然代码停止工作(甚至与 Azure Devops 在其文档中提供的相同示例)。

我尝试撤销现有的 PAT 并生成新的 PAT,但仍然无效。

public static async void PrintBuilds()
{
       try
       {
           var personalaccesstoken = "PAT";

           using (HttpClient client = new HttpClient())
           {
               client.DefaultRequestHeaders.Accept.Add(
                   new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

               client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                   Convert.ToBase64String(
                       System.Text.ASCIIEncoding.ASCII.GetBytes(
                           string.Format("{0}:{1}", "", personalaccesstoken))));

               using (HttpResponseMessage response = client.GetAsync(
                    "https://dev.azure.com/{Organization}/{Projcet}/_apis/build/builds?api-version=5.0").Result)
               {
                    esponse.EnsureSuccessStatusCode();
                   string responseBody = await response.Content.ReadAsStringAsync();
                   Console.WriteLine(responseBody);
               }
           }
       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.ToString());
       }
}

我希望得到 JSON 输出,但我得到 HTTP 状态 302,即重定向到 visualstudio.com 登录页面 - 这可能表明我的 PAT 有问题...

【问题讨论】:

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


    【解决方案1】:

    此代码表明您没有将用户名传递给 auth 标头,而您应该这样做,这是我的 powershell 代码的样子:

    $pat = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("user@domain:$token"))
    

    【讨论】:

    • 谢谢,就是这样。很奇怪,它以前工作过。
    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 2021-11-15
    • 2020-08-31
    • 2023-02-22
    • 2021-12-24
    • 2020-07-27
    • 2021-03-21
    • 2019-08-24
    相关资源
    最近更新 更多