0
votes

I was able to use Azure Devops RestAPI just fine, but suddently the code stopped working (even same example which Azure Devops provides in their documentation).

I tried to revoke existing PAT and generate new one, still didn't work.

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());
       }
}

I expect a JSON output, but I get HTTP status 302, i.e. a redirection to visualstudio.com signin page - which might suggest that there's something about my PAT...

1

1 Answers

2
votes

this code suggests you are not passing username to the auth header, while you should do that, here's what my powershell code looks like:

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