gpt4 book ai didi

azure - 使用rest api从azure devops存储库获取文件

转载 作者:行者123 更新时间:2023-12-04 09:38:00 24 4
gpt4 key购买 nike

我尝试使用以下代码从 AzureDevops 存储库获取特定文件:

public static async void GetFile()
{
try
{
var personalaccesstoken = "XXXXXXXXX";

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}", "", personalaccesstoken))));

using (HttpResponseMessage response = client.GetAsync(
"https://dev.azure.com/MS-ADM/SAPBuild/_apis/git/repositories/SAPBuild/items?path=/TestResult&download=true&api-version=5.0").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

但是我得到了以下 json:

{
"objectId": "08205d1ecfa86e4d8a2451fa189e68711398f126",
"gitObjectType": "tree",
"commitId": "xxxxxxxxxxxxxxxx",
"path": "/TestResult",
"isFolder": true,
"url": "https://dev.azure.com/MS-ADM/xxxxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/items?path=%2FTestResult&versionType=Branch&versionOptions=None",
"_links": {
"self": {
"href": "https://dev.azure.com/MS-ADM/xxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/items?path=%2FTestResult&versionType=Branch&versionOptions=None"
},
"repository": {
"href": "https://dev.azure.com/MS-ADM/xxxxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d"
},
"tree": {
"href": "https://dev.azure.com/MS-ADM/xxxxxxx/_apis/git/repositories/768454e2-4250-4d83-821b-7ac1c2707f5d/trees/08205d1ecfa86e4d8a2451fa189e68711398f126"
}
}
}

我应该在 url 中写什么来获取 TestResult 文件夹中的文件?

最佳答案

您正在使用的 API “https://dev.azure.com/MS-ADM/SAPBuild/_apis/git/repositories/SAPBuild/items?path=/TestResult&download=true&api-version=5.0

您应该指定特定的文件路径而不是文件夹路径,例如 path=path=%2FTestResult/**Home.cshtml**

使用文件夹路径,如您所见,返回 json 还显示 "isFolder": true,

更多详细信息和powershell示例,您可以引用以下示例:

关于azure - 使用rest api从azure devops存储库获取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62464734/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com