如何使用 Visual Studio Team Services API

本文关键字:Services API Team Studio 何使用 Visual | 更新日期: 2023-09-27 17:56:58

我已经破解了如何使用API拉取数据,但现在我正在尝试将新的工作项推送到项目中,但我似乎无法让它工作这是我的c#:

  try {
        using (HttpClient client = new HttpClient()) {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
             ASCIIEncoding.ASCII.GetBytes(
                string.Format("{0}:{1}", "", token))));
        string Url = $"https://XXX/DefaultCollection/{Job.Project}/_apis/wit/workitems/$Task?api-version=1.0";
        var method = new HttpMethod("PATCH");
        var request = new HttpRequestMessage(method, Url) {
               Content = new StringContent(Job.WorkItem, Encoding.UTF8,
                            "application/json-patch+json")
                    };
        using (HttpResponseMessage response = client.SendAsync(request).Result) {
                        response.EnsureSuccessStatusCode();
                        string responseBody = response.Content.ReadAsStringAsync().Result;                        
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString());
                return null;
            }

这是调试器中显示的 Job.WorkItem

"[
    {
        '"op'":'"add'",
        '"path'":'"/fields/System.Title'",
        '"value'":'"Please add a business area for XXX Systems''n'"
    }
]"

我是否错过了什么或做错了什么,因为它的回应是

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  X-TFS-ProcessId: xxx
  Strict-Transport-Security: max-age=31536000; includeSubDomains
  Access-Control-Allow-Origin: *
  Access-Control-Max-Age: 3600
  Access-Control-Allow-Methods: OPTIONS,GET,POST,PATCH,PUT,DELETE
  Access-Control-Expose-Headers: ActivityId,X-TFS-Session,X-MS-ContinuationToken
  Access-Control-Allow-Headers: authorization
  X-FRAME-OPTIONS: SAMEORIGIN
  X-VSS-UserData: xxxx
  ActivityId: xxxx
  X-TFS-Session: xxx
  X-Content-Type-Options: nosniff
  Cache-Control: no-cache
  Date: Wed, 13 Jul 2016 16:48:28 GMT
  P3P: CP="CAO DSP COR ADMa DEV CONo TELo CUR PSA PSD TAI IVDo OUR SAMi BUS DEM NAV STA UNI COM INT PHY ONL FIN PUR LOC CNT"
  Server: Microsoft-IIS/8.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 429
  Content-Type: application/json; charset=utf-8
  Expires: -1
}}

我省略了一些细节 xxx 仅供参考

如何使用 Visual Studio Team Services API

有关特定操作,请参阅 REST API 文档。

另外,当您从.net调用它时,也许VSO Api的客户端库是一种更简单的方法?

Job.WorkItem 中有多余的双引号,这将导致错误的请求。内容应为:

[
    {
        '"op'":'"add'",
        '"path'":'"/fields/System.Title'",
        '"value'":'"Please add a business area for XXX Systems''n'"
    }
]

有关详细信息,请参阅此链接:创建工作项。