2016-10-03 105 views
0

我想我使用它有一个用户名和密码,以下为“关于炳”获得从项目列表TFS如何使用TFS REST API

private static async void Method() 
     { 
      try 
      { 


       using (HttpClient 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://test-test-app1:8080/tfs/boc_projects/_apis/projects?api-version=2").Result) 
        { 
         response.EnsureSuccessStatusCode(); 
         string responseBody = await response.Content.ReadAsStringAsync(); 
         Console.WriteLine(responseBody); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
     } 

访问团队项目列表或Git项目名单管理员权限在TFS我想连接。但是当我尝试上述时,我得到未经授权的访问错误。

+0

我推荐使用目录服务,而不是REST API,仅仅是因为它不需要管理员权限。如果你有兴趣,我有代码。 –

+0

告诉我有关Catalog Service的更多信息,我不知道它 –

回答

1
  1. getting a list of team projects的REST API是:

>

http://tfsserver:8080/tfs/CollectionName/_apis/projects?api-version=1.0 
  • 请确认您已经启用了基本验证了您的TFS:

    • 检查您的IIS以查看基本身份验证服务角色已安装。
    • 转到IIS管理器,选择团队基础服务器 - 身份验证 并禁用基本身份验证以外的所有其他操作。然后在Team Foundation Server下执行 相同的tfs节点。
    • 重新启动您的IIS。
  • enter image description here

    enter image description here

    +0

    感谢您的回复,它现在正在工作。 –

    0

    下面是使用目录服务的简单应用程序。它通过循环遍历所有项目集合和项目来查找文件,并按名称查找文件的实例。根据您的需求改变它并不需要太多。

    using System; 
    using System.Linq; 
    using Microsoft.TeamFoundation.Common; 
    using Microsoft.TeamFoundation.Client; 
    using Microsoft.TeamFoundation.Framework.Client; 
    using Microsoft.TeamFoundation.Framework.Common; 
    using Microsoft.TeamFoundation.VersionControl.Client; 
    
    namespace EpsiFinder 
    { 
        internal class Program 
        { 
         // Server URL. Yes, it's hardcoded. 
         public static string Url = @"http://tfs.someserver.com:8080/tfs"; 
    
        private static void Main() 
        { 
         // Use this pattern search for the file that you want to find 
         var filePatterns = new[] { "somefile.cs" }; 
    
         var configurationServerUri = new Uri(Url); 
         var configurationServer = TfsConfigurationServerFactory.GetConfigurationServer(configurationServerUri); 
         var configurationServerNode = configurationServer.CatalogNode; 
    
         // Query the children of the configuration server node for all of the team project collection nodes 
         var tpcNodes = configurationServerNode.QueryChildren(
           new[] { CatalogResourceTypes.ProjectCollection }, 
           false, 
           CatalogQueryOptions.None); 
    
         // Changed to use the Catalog Service, which doesn't require admin access. Yay. 
         foreach (var tpcNode in tpcNodes) 
         { 
          Console.WriteLine("Collection: " + tpcNode.Resource.DisplayName + " - " + tpcNode.Resource.Description); 
    
          // Get the ServiceDefinition for the team project collection from the resource. 
          var tpcServiceDefinition = tpcNode.Resource.ServiceReferences["Location"]; 
          var configLocationService = configurationServer.GetService<ILocationService>(); 
          var newUrl = new Uri(configLocationService.LocationForCurrentConnection(tpcServiceDefinition)); 
    
          // Connect to the team project collection 
          var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(newUrl); 
    
          // This is where we can do stuff with the team project collection object 
    
          // Get the Version Control instance 
          var versionControl = tfs.GetService<VersionControlServer>(); 
          // Select the branches that match our criteria 
          var teamBranches = versionControl.QueryRootBranchObjects(RecursionType.Full) 
                  .Where(s => !s.Properties.RootItem.IsDeleted) 
                  .Select(s => s.Properties.RootItem.Item) 
                  .ToList(); 
          // Match the file in the branches, spit out the ones that match 
          foreach (var item in from teamBranch in teamBranches 
               from filePattern in filePatterns 
               from item in 
                versionControl.GetItems(teamBranch + "/" + filePattern, RecursionType.Full) 
                    .Items 
               select item) 
           Console.WriteLine(item.ServerItem); 
         } 
        } 
    } 
    

    }