[英]Get a list of team projects using REST api in C# for on premise TFS 2015 Update3
我正在嘗試使用來自C#的REST api調用並遵循以下MSDN文檔來獲取所有項目:
https://www.visualstudio.com/zh-CN/docs/integrate/api/tfs/projects
在執行GetTeamProjects()
我得到以下響應:
響應{狀態代碼:404,原因短語:'未找到',版本:1.1,內容:System.Net.Http.StreamContent,標頭:
我假設錯誤可能是由於身份驗證類型引起的。 我在內部使用NTLM時通過了Basic。
我正在嘗試獲取TFS項目以獲取用戶權限詳細信息。
我只是使用此方法而無需啟用基本身份驗證:
var client = new WebClient();
client.Credentials = new NetworkCredential("user", "password", "domain");
var response = client.DownloadString("http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2");
如果您遇到身份驗證問題,則應該收到401錯誤,而不是404錯誤。我擔心您的代碼有問題。 您可以在下面參考我的代碼:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace GetTeamProjectREST
{
class Program
{
public static void Main()
{
Task t = GetTeamProjectREST();
Task.WaitAll(new Task[] { t});
}
private static async Task GetTeamProjectREST()
{
try
{
var username = "domain\\username";
var password = "password";
using (var 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(
"http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.