Getting "Unauthorized" status code when requesting TFS REST API via C# Console Application and TFS 2015 Update 3.
Hello everybody,
I'm trying to control the TFS (2015 Update 3) REST API with a C# console application.
In my old application I referenced the Microsoft.VisualStudio.Build.Client
(etc.) libraries and could access the information I needed via the TfsTeamProjectCollection
. I honestly don't know where the authentication comes from. Maybe via the Windows credentials or the account associated with Visual Studio. In any case, I didn't have to specify credentials in my application.
But I didn't find an example for the REST API where I don't have to specifically authorize.
I found an example of using the .NET client library that authorizes itself with VssCredentials (using NTLM). I don't have to specify a username or oassword here either:
VssConnection connection = new VssConnection(new Uri(URL), new VssCredentials());
That works too, but I can't get all the information I need about it, so I wanted to control the REST API myself.
Can someone send me an example of how I can access the REST API without a separate username/password? Or is this no longer possible?
Thank you Johannes
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage response = await client.GetAsync("http://ServerName:8080/tfs/CollectionName/ProjectName/_apis/build/builds/BuildID"))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
I expect to get the json response but instead I only get the Unathorized Exception.