Azure Devops 个人访问令牌 (PAT) 停止工作

Azure Devops Personal Access Token (PAT) stopped working

我能够很好地使用 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 有问题...

此代码表明您没有将用户名传递给身份验证 header,而您应该这样做,这是我的 powershell 代码的样子:

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