[英]How to get list of Azure Batch Pools and Jobs using REST API in C#?
Using REST API I want to get the list of Batch Pools and Jobs.使用 REST API 我想获取批处理池和作业的列表。
As per the docs:根据文档:
Pool - Get |游泳池 - 获取 | Microsoft Docs -https://docs.microsoft.com/en-us/rest/api/batchservice/pool/get
微软文档 -https://docs.microsoft.com/en-us/rest/api/batchservice/pool/get
Job - Get |工作 - 获取 | Microsoft Docs - https://docs.microsoft.com/en-us/rest/api/batchservice/job/get
Microsoft Docs - https://docs.microsoft.com/en-us/rest/api/batchservice/job/get
The API to get list of job is GET {batchUrl}/jobs?api-version=2019-08-01.10.0
and to get pool is GET {batchUrl}/pools?api-version=2019-08-01.10.0
获取作业列表的 API 是
GET {batchUrl}/jobs?api-version=2019-08-01.10.0
,获取池的 API 是GET {batchUrl}/pools?api-version=2019-08-01.10.0
In C# I am doing it like this:在 C# 中,我是这样做的:
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _accessToken);
using (var responseGet = client.GetAsync(api).Result) //HttpClient client
{
if (responseGet.IsSuccessStatusCode)
{
dynamic batchObjectsContent = JObject.Parse(responseGet.Content.ReadAsStringAsync().Result);
foreach (var batchObject in batchObjectsContent.value)
{
batchObjects.Add(new BatchObject { Id = batchObject.id, Url = batchObject.url, CreationTime = batchObject.creationTime, StateTransitionTime = batchObject.stateTransitionTime });
}
}
}
The complete API for getting the pool is https://mybatch.westus2.batch.azure.com/pools?api-version=2019-08-01.10.0
and api for the job is https://mybatch.westus2.batch.azure.com/jobs?api-version=2019-08-01.10.0
.获取池的完整 API 是
https://mybatch.westus2.batch.azure.com/pools?api-version=2019-08-01.10.0
和作业的 api 是https://mybatch.westus2.batch.azure.com/jobs?api-version=2019-08-01.10.0
。
Error message I am getting:
StatusCode=Unauthorized
ReasonPhrase="Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly."
error="invalid_audience", error_description="The access token has been obtained from wrong audience or resource 'https://management.azure.com/'. It should exactly match (including forward slash) with one of the allowed audiences 'https://batch.core.windows.net/'"
And this is how I got the access token: authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential).Result.AccessToken;
这就是我获得访问令牌的方式:
authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential).Result.AccessToken;
. . This works for all the APIs related to
https://management.azure.com/
.这适用于与
https://management.azure.com/
相关的所有 API。
From the errors I think there is issue with either the access token or the headers are wrong, or both.从错误中,我认为访问令牌或标头错误或两者都有问题。 How do I correct them ?
我该如何纠正它们?
Use the Azure Batch resource endpoint to acquire a token for authenticating requests to the Batch service:使用 Azure Batch 资源终结点获取令牌以验证对 Batch 服务的请求:
https://batch.core.windows.net/
Use the code as below:使用代码如下:
private const string BatchResourceUri = "https://batch.core.windows.net/";
AuthenticationResult authResult = await authContext.AcquireTokenAsync(BatchResourceUri, new ClientCredential(ClientId, ClientKey));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.