我正在尝试使用spring boot .Previously向azure迁移中的craete项目发出PUT请求,我正在使用Rest模板进行迁移。如@Autowire RestTemplate模板;
新标头= HttpHeaders HttpHeaders();headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type", assessment.getGrant_type());
map.add("client_id", assessment.getClient_id());
map.add("client_secret", assessment.getClient_secret());
map.add("code", authToken);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, request, String.class);authToken我得到了在POST调用和POST标题中设置之前对身份验证URL进行单独的GET调用。
我发布了部分代码,这样你就可以知道我在做什么。
现在我被要求使用Azure-sdk for java来编写同样的代码。我不需要使用Rest Template.Can,任何人告诉我如何使用Azure-sdk进行java类的POST调用。我在谷歌和you tube上搜索,但没有具体的发现。
致敬,Prabhash Mishra
发布于 2020-04-17 10:17:20
如果您想使用Azure java sdk调用Azure rest API,请参考以下代码
a.创建服务主体(我使用Azure CLI来完成此操作)
az login
az account set --subscription "<your subscription id>"
# the sp will have Azure Contributor role
az ad sp create-for-rbac -n "readMetric"

<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure -->
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure</artifactId>
<version>1.33.0</version>
</dependency> private static String tenantId="hanxia.onmicrosoft.com"; // sp tenant
private static String clientId = "42e0d080-b1f3-40cf-8db6-c4c522d988c4"; // sp appid
private static String clientKey = "Gbx*************JDfQpIjoae:";// sp password
private static String subscriptionId="e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68"; //sp subscription id
ApplicationTokenCredentials creds = new
ApplicationTokenCredentials(clientId,tenantId,clientKey, AzureEnvironment.AZURE);
RestClient restClient =new RestClient.Builder()
.withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
.withSerializerAdapter(new AzureJacksonAdapter())
.withReadTimeout(150, TimeUnit.SECONDS)
.withLogLevel(LogLevel.BODY)
.withResponseBuilderFactory(new AzureResponseBuilder.Factory())
.withCredentials(creds)
.build();
OkHttpClient httpClient = restClient.httpClient().newBuilder().build();
String url="https://management.azure.com/subscriptions/"+subscriptionId+"/providers/Microsoft.Migrate/projects?api-version=2018-02-02";
Request request = new Request.Builder()
.url(url)
.method("get",null)
.build();
Response response1 = httpClient.newCall(request).execute();
if(response1.isSuccessful()){
System.out.println(response1.headers().toString());
}

https://stackoverflow.com/questions/61241880
复制相似问题