2016-09-22 75 views
1

我目前正在嘗試使用Visual Studio Team Services公司提供的其他API(Visual Studio Online)來獲取工作項目信息。我似乎可以連接,但是當我查看對我的查詢的響應時,它帶有增強型安全錯誤消息的html頁面。我相信這是由於IE中的增強安全選項,但我從我的客戶端計算機上調用了此選項,我只能看到有關如何在服務器上關閉此選項的選項。Visual Studio Team Services Rest增強的安全性錯誤API

這是我正在做

using (var client = new HttpClient()) 
     { 
      var token =  "xxxxxxxxxxxx"; 
      var apiVersion = "1.0"; 

      var account = "xxxxxxxx"; 
      var query = "Select [System.Id] From WorkItems Where[System.WorkItemType] = 'WorkItem' order by [System.CreatedDate] desc"; 

      var url = "https://" + account + ".visualstudio.com/Core/_apis/wit/"; 

      // Execute a query that returns work item IDs matching the specified criteria 
      using (var request = new HttpRequestMessage(HttpMethod.Post, url + "wiql")) 
      { 
       request.Headers.Add("Authorization", "Bearer " + token); 
       request.Headers.Add("Accept", "application/json;api-version=" + apiVersion); 

       Dictionary<string, string> body = new Dictionary<string, string> 
      { 
       { 
        "query", query 
        } 
      }; 

       request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json"); 

       using (var response = await client.SendAsync(request)) 
       { 
        var content = await response.Content.ReadAsStringAsync(); 
        var workItems = JObject.Parse(content)["workItems"] as JArray; 

        string[] ids = workItems.Select<JToken, string>(w => (w["id"] + "")).Take(10).ToArray<string>(); 
        string idsString = String.Join(",", ids); 

        // Get details for the last 10 
        using (var detailsRequest = new HttpRequestMessage(HttpMethod.Get, url + "workitems?ids=" + idsString + "&fields=System.Id,System.Title")) 
        { 
         detailsRequest.Headers.Add("Authorization", "Bearer " + token); 
         detailsRequest.Headers.Add("Accept", "application/json;api-version=" + apiVersion); 

         using (var detailsResponse = await client.SendAsync(detailsRequest)) 
         { 
          var detailsContent = await detailsResponse.Content.ReadAsStringAsync(); 
          var detailsWorkItems = JObject.Parse(detailsContent)["value"] as JArray; 

          foreach (dynamic workItem in detailsWorkItems) 
          { 
           Console.WriteLine("Work item: {0} ({1})", 
            workItem.fields["System.Id"], 
            workItem.fields["System.Title"] 
           ); 
          } 
         } 
        } 
       } 
      } 
     } 

任何幫助,這將不勝感激,呼叫

感謝

克里斯

+0

可以共享錯誤或者截圖? –

+0

我調用sendasync方法時的響應只是一個html頁面。在檢查時,它提供了嚴重的安全信息 – chrisblue13

+0

如果禁用Internet Explorer增強的安全配置,它是否工作? –

回答

0

可以相關網站添加到受信任網站(例如:https://app.vssps.visualstudio.com,https://login.live.com等...)。

  1. Internet選項=>安全
  2. 選擇可信站點
  3. 點擊網站
  4. 類型的網站地址,然後單擊添加

簡單的辦法知道哪些URL需要添加,你可以發送簡單的Get Rest請求(例如獲取工作項REST API),它會彈出一個包含站點URL的窗口(將針對不同URL彈出多次),將這些URL添加到可信站點列表中。 enter image description here

更新:

基於響應結果,它是不相關的增強的安全性,該結果意味着它沒有通過認證。所以令牌無效,它是OAuth的訪問令牌,您需要在將您的應用註冊到VSTS後獲取訪問令牌。

更多信息,可以參考this文章。

There是您可以參考的OAuth示例。獲取訪問令牌後,將其添加到請求標頭並從VSTS檢索數據。

如果你想通過個人訪問令牌訪問VSTS,這樣的代碼:(檢查this文章)

try 
    { 
     var username = "username"; 
     var password = "password"; 

     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(
         "https://{account}.visualstudio.com/DefaultCollection/_apis/build/builds").Result) 
      { 
       response.EnsureSuccessStatusCode(); 
       string responseBody = await response.Content.ReadAsStringAsync(); 
       Console.WriteLine(responseBody); 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.WriteLine(ex.ToString()); 
    } 
+0

感謝您的幫助。我已將相關網址添加到受信任的網站,但它仍然無效。 – chrisblue13

+0

@ chrisblue13如果您通過Internet Explorer發送REST請求,結果如何? (https://www.visualstudio.com/en-us/docs/integrate/api/wit/work-items#by-ids) –

+0

通過瀏覽器正常工作。只是不通過視覺工作室。 – chrisblue13

相關問題