Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TeamCity - Unable to authenticate via API

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?

like image 866
frods Avatar asked Jun 29 '15 13:06

frods


1 Answers

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);
like image 69
Jagadeesh Govindaraj Avatar answered Oct 11 '22 08:10

Jagadeesh Govindaraj