如何在 C# 中使用 REST API 获取 Azure 批处理池和作业的列表?
How to get list of Azure Batch Pools and Jobs using REST API in C#?
使用 REST API 我想获取批处理池和作业的列表。
根据文档:
池-获取| Microsoft 文档 - https://docs.microsoft.com/en-us/rest/api/batchservice/pool/get
工作 - 获取 | Microsoft 文档 - https://docs.microsoft.com/en-us/rest/api/batchservice/job/get
获取作业列表的 API 是 GET {batchUrl}/jobs?api-version=2019-08-01.10.0
,获取池的 GET {batchUrl}/pools?api-version=2019-08-01.10.0
在 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 });
}
}
}
获取池的完整 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/'"
这就是我获得访问令牌的方式:authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential).Result.AccessToken;
。这适用于与 https://management.azure.com/
.
相关的所有 API
根据错误,我认为访问令牌或 headers 有问题,或两者都有问题。我该如何更正它们?
使用 Azure Batch 资源终结点获取令牌以对 Batch 服务请求进行身份验证:
https://batch.core.windows.net/
使用如下代码:
private const string BatchResourceUri = "https://batch.core.windows.net/";
AuthenticationResult authResult = await authContext.AcquireTokenAsync(BatchResourceUri, new ClientCredential(ClientId, ClientKey));
参考这个article。
使用 REST API 我想获取批处理池和作业的列表。
根据文档:
池-获取| Microsoft 文档 - https://docs.microsoft.com/en-us/rest/api/batchservice/pool/get
工作 - 获取 | Microsoft 文档 - https://docs.microsoft.com/en-us/rest/api/batchservice/job/get
获取作业列表的 API 是 GET {batchUrl}/jobs?api-version=2019-08-01.10.0
,获取池的 GET {batchUrl}/pools?api-version=2019-08-01.10.0
在 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 });
}
}
}
获取池的完整 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/'"
这就是我获得访问令牌的方式:authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential).Result.AccessToken;
。这适用于与 https://management.azure.com/
.
根据错误,我认为访问令牌或 headers 有问题,或两者都有问题。我该如何更正它们?
使用 Azure Batch 资源终结点获取令牌以对 Batch 服务请求进行身份验证:
https://batch.core.windows.net/
使用如下代码:
private const string BatchResourceUri = "https://batch.core.windows.net/";
AuthenticationResult authResult = await authContext.AcquireTokenAsync(BatchResourceUri, new ClientCredential(ClientId, ClientKey));
参考这个article。