Azure Java SDK 用于以编程方式获取令牌

huangapple go评论64阅读模式
英文:

azure java sdk for az get token programatically

问题

我正在学习Azure Java SDK,但我没有找到用于以下命令的包装方法:

az account get-access-token --resource api://xxxxx.microsoft.com/YYYY/ZZZZ

这个请求在Windows批处理中可以正常工作,但我无法使用Azure Java SDK运行它。我会感激任何关于如何使用Azure Java SDK运行此命令的反馈。

最好的问候,
Aurelian

英文:

I am studying the azure java sdk and I did not find a wrapper method for

az account get-access-token --resource api://xxxxx.microsoft.com/YYYY/ZZZZ

This request works properly from windows batch but i cannot run using azure java sdk.
I'll appreciate any feedback about how to run this command using azure java sdk

Best Regards,
Aurelian

答案1

得分: 1

以下是您要翻译的代码部分:

>azure java sdk for az get token programatically:-

You can use the below java spring-boot code to retrieve the access token and it worked for me as follows.

`RestApiController.java`

```bash
package com.myapp.restcall.controller;  
import java.util.Collections;  
import org.json.JSONObject;  
import org.springframework.http.HttpEntity;  
import org.springframework.http.HttpHeaders;  
import org.springframework.http.HttpMethod;  
import org.springframework.http.MediaType;  
import org.springframework.http.ResponseEntity;  
import org.springframework.util.LinkedMultiValueMap;  
import org.springframework.util.MultiValueMap;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.client.RestTemplate;  
  
@RestController  
public class RestApiController {  
  
   @GetMapping(value = "/gettoken")  
   public static String getToken() throws Exception {  
   String url = "https://login.microsoftonline.com/<tenantID>/oauth2/token";  
  HttpHeaders headers = new HttpHeaders();  
  headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  
  MultiValueMap<String, String> inputMap = new LinkedMultiValueMap<>();  
  
  inputMap.add("grant_type", "client_credentials");  
  inputMap.add("client_id", "CLIENT_ID");  
  inputMap.add("client_secret", "CLIENT_SECRET");  
  inputMap.add("resource", "https://management.core.windows.net/");  
  HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(inputMap,  
  headers);  
  RestTemplate template = new RestTemplate();  
  ResponseEntity<String> response = template.postForEntity(url, entity, String.class);  
  JSONObject myjson = new JSONObject(response.getBody());  
  String data = (String) myjson.get("access_token");  
  System.out.println(data);  
  String access_token = data;  
 return access_token;  
  
  }  
}

在执行上述Java代码之前,请替换其中的CLIENT_IDCLIENT_SECRETTENANT_ID

要构建程序,请在IDE终端中使用以下命令:

mvn clean install
mvn spring-boot:run

localhost:8080/gettoken 成功生成令牌

Azure Java SDK 用于以编程方式获取令牌

Token generated at localhost:8080/gettoken successfully.

Azure Java SDK 用于以编程方式获取令牌


<details>
<summary>英文:</summary>
&gt;azure java sdk for az get token programatically:-
You can use the below java spring-boot code to retrieve the access token and it worked for me as follows.
`RestApiController.java`
```bash
package com.myapp.restcall.controller;  
import java.util.Collections;  
import org.json.JSONObject;  
import org.springframework.http.HttpEntity;  
import org.springframework.http.HttpHeaders;  
import org.springframework.http.HttpMethod;  
import org.springframework.http.MediaType;  
import org.springframework.http.ResponseEntity;  
import org.springframework.util.LinkedMultiValueMap;  
import org.springframework.util.MultiValueMap;  
import org.springframework.web.bind.annotation.GetMapping;  
import org.springframework.web.bind.annotation.RestController;  
import org.springframework.web.client.RestTemplate;  
@RestController  
public class RestApiController {  
@GetMapping(value = &quot;/gettoken&quot;)  
public static String getToken() throws Exception {  
String url = &quot;https://login.microsoftonline.com/&lt;tenantID&gt;/oauth2/token&quot;;  
HttpHeaders headers = new HttpHeaders();  
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  
MultiValueMap&lt;String, String&gt; inputMap = new LinkedMultiValueMap&lt;&gt;();  
inputMap.add(&quot;grant_type&quot;, &quot;client_credentials&quot;);  
inputMap.add(&quot;client_id&quot;, &quot;CLIENT_ID&quot;);  
inputMap.add(&quot;client_secret&quot;, &quot;CLIENT_SECRET&quot;);  
inputMap.add(&quot;resource&quot;, &quot;https://management.core.windows.net/&quot;);  
HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt; entity = new HttpEntity&lt;MultiValueMap&lt;String, String&gt;&gt;(inputMap,  
headers);  
RestTemplate template = new RestTemplate();  
ResponseEntity&lt;String&gt; response = template.postForEntity(url, entity, String.class);  
JSONObject myjson = new JSONObject(response.getBody());  
String data = (String) myjson.get(&quot;access_token&quot;);  
System.out.println(data);  
String access_token = data;  
return access_token;  
}  
}

Replace CLIENT_ID, CLIENT_SECRET &amp; TENANT_ID in the above java code before executing it.

To build the program, use below commands in the IDE terminal.

mvn clean install
mvn spring-boot:run

Azure Java SDK 用于以编程方式获取令牌

Token generated at localhost:8080/gettoken successfully.

Azure Java SDK 用于以编程方式获取令牌

huangapple
  • 本文由 发表于 2023年8月4日 04:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定