到目前为止,我已经使用此代码来获取我们的azure devops项目数据。我四处查看数据,返回的所有网址都在寻找与管道相关的数据,却一无所获
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));
ListofProjectsResponse.Projects viewModel = null;
//use the httpclient
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://acme.visualstudio.com"); //url of our account
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
HttpResponseMessage response = client.GetAsync("/DefaultCollection/_apis/projects?stateFilter=All&api-version=1.0").Result;
//check to see if we have a succesfull respond
if (response.IsSuccessStatusCode)
{
//set the viewmodel from the content in the response
viewModel = response.Content.ReadAsAsync<ListofProjectsResponse.Projects>().Result;
}
}
public class ListofProjectsResponse
{
public class Projects
{
public int count { get; set; }
public Value[] value { get; set; }
}
public class Value
{
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string url { get; set; }
public string state { get; set; }
}
}
答案 0 :(得分:1)
对主要工作代码进行了一些更改并在下面共享,请尝试一下:
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "", "{PAT}")));
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://acme.visualstudio.com"); //url of our account
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
//HttpResponseMessage response = client.GetAsync("/_apis/projects?stateFilter=All&api-version=1.0").Result;
using (HttpResponseMessage response = await client.GetAsync("/ForMerlin/_apis/projects"))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
// Console.WriteLine(response);
}
对于BaseAddress
,您使用的是旧的URL格式,例如https://{org name}.visualstudio.com
。此URL格式中包含组织名称,因此调用organization name
时可以忽略GetAsync
。只需将其设为/_apis/projects?stateFilter=All&api-version=1.0
就可以了。
答案 1 :(得分:0)
我发现的方法是使用与上面相同的代码枚举每个项目的构建,
HttpResponseMessage response = client.GetAsync("/{org name}/{project
name}/_apis/build/builds?api-version=5.1").Result;
每个构建的url将返回一个json集合,其中包含“队列”内的任何天蓝色的构建点数据。