[英]Azure Devops REST API with C# Authentication
[英]Set lease for build with Azure DevOps API in C#
如果條件合適,我會打電話給我的 Azure DevOps 服務器,將構建設置為不被刪除。
我跟着: https://learn.microsoft.com/en-us/rest/api/azure/devops/build/leases/add?view=azure-devops-rest-6.0
我有一些 C# 代碼,我在其中嘗試為特定構建設置保留。 但是我在 HttpResponse 中收到了錯誤的請求。 由於錯誤的請求,我對 API 進行了其他幾次調用,我相信這不是權限。
我已經匿名添加了我的代碼。
using (var httpClient = new HttpClient(new HttpClientHandler()))
{
httpClient.DefaultRequestHeaders.Accept.Add(new
MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new
AuthenticationHeaderValue("Basic", personalAccessToken);
JObject postBody = new JObject
{
{ "daysValid", 100 },
{ "ownerId", "user@domain.com" },
{ "definitionId", 2222 },
{ "protectPipeline", true },
{ "runId", 4444 }
};
HttpContent content = new StringContent(postBody.ToString(),
Encoding.UTF8, "application/json");
using HttpResponseMessage response = await
httpClient.PostAsync($"https://dev.azure.com/{organization}/{project}/_apis/build/retention/leases?api-version=6.0-preview.1",
content).ConfigureAwait(false);
if (!response.IsSuccessStatusCode)
{
}
}
解決方案其實很簡單。 至少當你知道的時候。 我提供的內容數據應該是一個數組,所以我改成之后:
JArray array = new JArray { postBody };
HttpContent content = new StringContent(JsonConvert.SerializeObject(array), Encoding.UTF8, "application/json");
它運行良好,我現在可以看到我的新租約已添加到構建中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.