gpt4 book ai didi

c# - 如何在 C# 中使用 REST API 获取 Azure 批处理池和作业列表?

转载 作者:行者123 更新时间:2023-12-03 05:40:20 24 4
gpt4 key购买 nike

使用 REST API 我想获取批处理池和作业的列表。
根据文档:
池 - 获取 |微软文档-https://learn.microsoft.com/en-us/rest/api/batchservice/pool/get
工作 - 获取 |微软文档-https://learn.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/'"

这就是我获取访问 token 的方式:authenticationContext.AcquireTokenAsync("https://management.azure.com/", credential).Result.AccessToken;。这适用于与 https://management.azure.com/ 相关的所有 API。

根据错误,我认为访问 token 或 header 错误或两者都有问题。我该如何纠正它们?

最佳答案

使用 Azure Batch 资源终结点获取用于验证对 Batch 服务的请求的 token :

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 .

关于c# - 如何在 C# 中使用 REST API 获取 Azure 批处理池和作业列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59210134/

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