获取使用C#中的REST api进行内部部署TFS 2015 Update3的团队项目列表

时间:2017-03-06 06:41:28

标签: c# rest tfs

我正在尝试使用来自c#的REST api调用来获取所有项目,并遵循以下MSDN文档:

  

https://www.visualstudio.com/en-us/docs/integrate/api/tfs/projects

执行GetTeamProjects()时,我收到以下回复:

  

响应{StatusCode:404,ReasonPhrase:' Not Found',版本:1.1,   内容:System.Net.Http.StreamContent,Headers:

我假设错误可能是由于身份验证类型。我正在通过Basic,而我的内部使用NTLM。

我正在尝试获取TFS项目以获取用户权限详细信息。

2 个答案:

答案 0 :(得分:1)

我只需使用它而无需启用基本身份验证:

var client = new WebClient();
client.Credentials = new NetworkCredential("user", "password", "domain");
var response = client.DownloadString("http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2");

答案 1 :(得分:0)

如果您遇到身份验证问题,则应该收到401错误,而不是404.我担心您的代码出现问题。您可以参考下面的代码:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace GetTeamProjectREST
{
    class Program
    {
        public static void Main()
        {
            Task t = GetTeamProjectREST();
            Task.WaitAll(new Task[] { t});
        }
        private static async Task GetTeamProjectREST()
        {
            try
            {
                var username = "domain\\username";
                var password = "password";

                using (var 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}", username, password))));

                    using (HttpResponseMessage response = client.GetAsync(
                                "http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2").Result)
                    {
                        response.EnsureSuccessStatusCode();
                        string responseBody = await response.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}