c# - TFS Rest API 查询

标签 c# tfs azure-devops-rest-api tfs-sdk

我们正在使用 TFS 2015,并将迁移到 TFS 2017 版本。

由于我们有很多集合(大约 17 个),有时我们必须手动检查所有集合中的值。为了缓解这个问题,我计划查询 TFS Rest API,以获取值。

我对如何使用 Rest API 有一些疑问,因为我们可以使用以下方法:

1.) 使用 Nuget 包,例如:

Microsoft.TeamFoundationServer.Client

Microsoft.VisualStudio.Services.Client

Microsoft.VisualStudio.Services.InteractiveClient

这需要以下代码:

//Prompt user for credential
VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssBasicCredential(string.Empty, pat));

//create a wiql object and build our query
Wiql wiql = new Wiql()
{
    Query = "Select [State], [Title] " +
            "From WorkItems " +
            "Where [Work Item Type] = 'Bug' " +
            "And [System.TeamProject] = '" + project + "' " +
            "And [System.State] <> 'Closed' " +
            "Order By [State] Asc, [Changed Date] Desc"
};

//create http client and query for resutls
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Bug' AND [Assigned To] = @Me" };
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;

//Display reults in console
if (queryResults == null || queryResults.WorkItems.Count() == 0)
{
    Console.WriteLine("Query did not find any results");
}

2.) 另一种方法是使用带有 PAT 或备用凭证的 HttpClient 访问 Rest API,如下所示:

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
            $"{username}:{password}")));

    using (HttpResponseMessage response = client.GetAsync(
        $"https://dev.azure.com/{account}/_apis/projects").Result)
    {
        response.EnsureSuccessStatusCode();
        repsonseBody = await response.Content.ReadAsStringAsync();
    }
}

这 2 种方法之间有什么区别?哪种方法是推荐的方法并且适用于 TFS 2017 版本?

任何帮助、建议或链接都​​会很棒。

OAuth 身份验证是否也可以与 TFS 一起使用,就像与 Azure Devops 一样使用吗?

最佳答案

nuget 包中的 *HttpClient 类是 REST API 的包装器。所以它们的级别更高,应该更容易使用。他们还为所有结果和参数对象定义类。由于它们针对相同的 API,因此受到同等支持。


Also does OAuth Authentication work with TFS, as it does with Azure Devops ?

不,它不符合 https://learn.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops

The following guidance is intended for Azure DevOps Services users, since OAuth 2.0 is not supported on Team Foundation Server or Azure DevOps Server.

关于c# - TFS Rest API 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57786521/

相关文章:

c# - 在单元测试项目中获取具有属性的程序集

tfs - 如何将源代码从一台 TFS 服务器迁移到另一台?

azure-devops - Azure Pipelines + Repos Rest API : Permissions for creating a Pull Request via API using System. AccessToken

Angular 10 DevOps 扩展 Rest Api

azure - 通过 REST API Powershell 脚本检查 "Allow all pipelines"

c# - 必须放在带有 runat=server 的表单标签内

c# - 因为它有一个 DefiningQuery 并且 <ModificationFunctionMapping> 元素中不存在 <InsertFunction> 元素

c# - ASP.Net C# 变量不存储在网页上

tfs - 当出现构建警告时,如何使 TFS 构建结果为 "Partially Succeeded"?

TFS 2010 : history lost after moving a folder