简体   繁体   English

如何使用访问令牌请求 microsoft azure 资源? 简而言之,我们如何在 azure 中使用生成的 access_token?

[英]How to request a microsoft azure resource using a access token? In short how can we use the generated access_token in azure?

I have registered an app in Azure.我已经在 Azure 中注册了一个应用程序。 I can get access_token from oauth2/v2.0/token from password grant.我可以通过密码授权从oauth2/v2.0/token获取access_token I can also get refresh_token from oauth2/v2.0/token using refresh_token grant But, How do I request a resource in Microsoft Azure using an access_token ?我还可以使用 refresh_token grant 从oauth2/v2.0/token获取refresh_token但是,如何使用access_token在 Microsoft Azure 中请求资源?

This process has been defined very clearly here: https://docs.microsoft.com/en-us/rest/api/azure .这个过程在这里定义得非常清楚: https : //docs.microsoft.com/en-us/rest/api/azure Especially look at the section titled Create the request which will tell you exactly what needs to be done.特别是查看标题为“ Create the request ”的部分Create the request它会准确地告诉您需要做什么。

In short how can we use the generated access_token in azure?简而言之,我们如何在 azure 中使用生成的 access_token?

When you would request anything on azure you have to pass your access token as your request header request.Headers.Authorization which is Bearer token.当您在 azure 上请求任何内容时,您必须将您的访问令牌作为您的请求标头request.Headers.Authorization传递,这是不Bearer令牌。

  • Here I am giving you an example how you could access azure resource using your access token.在这里,我为您提供了一个示例,说明如何使用您的访问令牌访问 azure 资源。

    //New Block For Accessing Data from Microsoft Graph Rest API
    HttpClient _client = new HttpClient();
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("https://graph.microsoft.com/v1.0/users"));
    //Passing Token For this Request
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Pass Your Token Here");
    HttpResponseMessage response = await _client.SendAsync(request);
    //Get User List From Response 
    dynamic objGpraphUserList = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());

I am accessing Azure Active Directory User List using access token .我正在使用access token访问Azure Active Directory User List I got this response我收到了这个回复

See the screenshot below:请看下面的截图:

在此处输入图片说明

Azure Resource API Access Sample : Azure 资源 API 访问示例:

As Per Gaurav Mantri's Recommendation I am also giving you another example how you could get azure resource group information.作为 Per Gaurav Mantri 的建议,我还提供了另一个示例,说明如何获取 azure 资源组信息。 See below code snippet见下面的代码片段

            //How You Would Request Microsft Resource Management API For Resources List 
            var  azureSubcriptionId = "Your_Azure_Subcription_Id";
            var resourceGroupName = "Your_Resource_Group_Name";
            HttpClient _client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/resources?api-version=2019-10-01", azureSubcriptionId, resourceGroupName));
            //Passing Token For this Request
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "Pass Your Token Here");
            HttpResponseMessage response = await _client.SendAsync(request);
            //Get User List From Response 
            dynamic objAzureResourceGroupList = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());

Also Tested on PostMan.也在 PostMan 上测试过。

在此处输入图片说明

For more details you could refer Official Docs更多细节可以参考官方文档

Hope this would help you.希望这会帮助你。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM