【问题标题】:TFS Rest API QueriesTFS 休息 API 查询
【发布时间】:2020-01-07 05:41:05
【问题描述】:

我们正在使用 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();
    }
}

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

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

OAuth 身份验证是否也适用于 TFS,就像它适用于 Azure Devops 一样?

【问题讨论】:

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


    【解决方案1】:

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


    OAuth 身份验证是否也适用于 TFS,就像它适用于 Azure Devops 一样?

    不,不是https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops

    以下指南适用于 Azure DevOps Services 用户,因为 Team Foundation Server 或 Azure DevOps Server 不支持 OAuth 2.0。

    【讨论】:

      【解决方案2】:

      我在我的项目中使用第一个选项。在 MS 的支持下,您可以提示用户的信用或使用 PAT。此外,您可以使用具有已知结果的明确方法,而无需定义自己的类。

      【讨论】:

        猜你喜欢
        • 2012-12-09
        • 1970-01-01
        • 1970-01-01
        • 2014-03-21
        • 2018-11-03
        • 2021-11-28
        • 1970-01-01
        • 2015-11-12
        • 1970-01-01
        相关资源
        最近更新 更多