使用PAT的TFS .Net REST API访问

时间:2019-06-23 15:27:29

标签: tfs azure-devops

我在C#中有一个代码可以访问TFS Rest API。 当我使用以下代码时:

VssConnection connection = new VssConnection(orgUrl, new VssCredentials());

我得到了所需的结果。

尝试设置凭据时:

VssConnection connection = new VssConnection(orgUrl, credentials);

我得到一个错误。

  

mscorlib.dll中发生了'System.AggregateException'类型的未处理异常

在提交以下代码行时发生错误:

WorkItem workitem = witClient.GetWorkItemAsync(workItemId).Result;

完整代码:

static void Main(string[] args)
{
            Uri orgUrl = new Uri("my uri");                       
            string personalAccessToken = "my token"; 
            int workItemId = 486915;   

        // Create a connection
        VssCredentials credentials = new VssBasicCredential("", personalAccessToken);
        VssConnection connection = new VssConnection(orgUrl, credentials);

        //VssConnection connection = new VssConnection(orgUrl, new VssCredentials());

        // Show details a work item
        ShowWorkItemDetails(connection, workItemId);

        Console.ReadLine();
}

public static void ShowWorkItemDetails(VssConnection connection, int workItemId)
{
        // Get an instance of the work item tracking client
        WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();

        // Get the specified work item
        WorkItem workitem = witClient.GetWorkItemAsync(workItemId).Result;

            // Output the work item's field values
            foreach (var field in workitem.Fields)
            {
                Console.WriteLine("  {0}: {1}", field.Key, field.Value);
            }
}

0 个答案:

没有答案