9

I've been struggling with authentication in TeamCity through the API lately. I can access the resources directly in my browser (http://usr:pw@teamcity:8111/httpAuth/app/rest/...), but doing so programmatically returns 401-Unauthorized.

WebRequest request = WebRequest.Create("http://user:pwd@teamcity:8111/httpAuth/app/rest/projects");
        request.Method = WebRequestMethods.Http.Get;
        try
        {
            request.Timeout = Timeout.Infinite;
             WebResponse response = request.GetResponse(); //Returns 401:Unauthorized

I can use guestAuth(http://teamcity:8111/guestAuth/app/rest/projects) without any problem, so there should not be any problem with the WebRequest itself.

Does anyone have an idea?

frods
  • 311
  • 1
  • 3
  • 10
  • .you should sent the client credentials.then only will be response sucess – Jagadeesh Govindaraj Jun 29 '15 at 13:25
  • I saw one example here which I thought was sufficient..(http://stackoverflow.com/questions/11017686/how-to-pass-username-and-password-in-teamcity-rest-api) – frods Jun 29 '15 at 13:30
  • @...this curl request look at this example ..esspecially for 'CreateHttpClient' https://github.com/stack72/TeamCitySharp/blob/master/src/TeamCitySharp/Connection/TeamCityCaller.cs – Jagadeesh Govindaraj Jun 29 '15 at 13:40

1 Answers1

7

Try to add your credentials and then make request.it will be get what you need.

    var username = "abc";
    var password = "123";
    var encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
    request.Headers.Add("Authorization", "Basic " + encoded);
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52
  • Thank you so much! Created the request based on another answer here, but I guess I'm using it in the wront context.. – frods Jun 29 '15 at 13:40