【发布时间】:2019-12-06 09:18:27
【问题描述】:
使用 REST API 我想获取批处理池和作业的列表。
根据文档:
游泳池 - 获取 |微软文档 - https://docs.microsoft.com/en-us/rest/api/batchservice/pool/get
工作 - 获取 |微软文档 - https://docs.microsoft.com/en-us/rest/api/batchservice/job/get
获取作业列表的 API 是 GET {batchUrl}/jobs?api-version=2019-08-01.10.0,获取池的 API 是 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。
从错误中我认为访问令牌或标头错误或两者都有问题。我该如何纠正它们?
【问题讨论】:
标签: c# azure rest http-headers azure-batch