简体   繁体   English

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

[英]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));

Refer to this article .参考这篇文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用适用于.Net 2 C#的REST API为Azure表服务实现批处理启动 - How to implement a batch start for Azure table service using REST API for .Net 2 C# 如何在 C# 中使用 JobStorage 获取所有 Hangfire 作业的列表? - How to get List of all Hangfire Jobs using JobStorage in C#? 使用C#的Azure REST Api身份验证 - Azure REST Api Authentication using C# 使用 azure devops API 在 C# 中更新发布和构建定义中的代理池 - Updating agent pools in release and build definitions using the azure devops API in C# 如何在C#中拨打Azure Rest API - How to call Azure Rest API in C# 使用 C# 列出 Azure 资源所需的确切权限\操作 rest API - Exact Permissions\actions needed to List Azure Resources using C# rest API C#使用REST API连接到Azure媒体服务帐户 - C# Connecting to Azure Media Services Account using REST API 如何获取 azure devops rest api 调用中所有测试运行的 azure 的延续令牌? - How to get continuation token for azure devops rest api calls in C# for fetching all test runs? Azure - C# - 使用 Rest 上传图像时出错 API - Azure - C# - Error while uploading image using Rest API 如何使用 C# REST API 查找和删除 Azure 存储容器? - How to find and delete Azure Storage Container using C# REST API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM