2017-03-06 52 views
0

我试图使用REST API调用从C#把所有的项目,并在按照以下MSDN文档:获得团队项目的列表2015年UPDATE3

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

当执行GetTeamProjects()我收到响应以下:

响应{的StatusCode:404,ReasonPhrase: '未找到',版本:1.1, 内容:System.Net.Http.StreamContent,标题:

我假设错误可能是由于验证类型。我通过基本,而我的前提使用NTLM。

我想获取TFS项目以获得用户权限的详细信息。

回答

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

你上面的代码给我下面的: 响应\t {的StatusCode:401,ReasonPhrase :'未经授权',版本:1.1,内容:System.Net.Http.StreamContent – Angshuman

+1

您需要在TFS机器上的IIS中启用基本身份验证。 –

+0

是否有可能用NTLM而不是基本操作。 不确定它是否是一个适当的问题。 – Angshuman

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");