英文:
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_ID
、CLIENT_SECRET
和TENANT_ID
。
要构建程序,请在IDE终端中使用以下命令:
mvn clean install
mvn spring-boot:run
在 localhost:8080/gettoken
成功生成令牌。
Token generated at localhost:8080/gettoken
successfully.
<details>
<summary>英文:</summary>
>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;
}
}
Replace CLIENT_ID, CLIENT_SECRET & 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
Token generated at localhost:8080/gettoken
successfully.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论