& 다른 SDK의 소스 코드를 조회했는데, 마침내는 REST API를 얻었다.
귀하의 필요에 맞는 REST API는 첫 번째 단계에서 다음과 같습니다. 헤더 Authorization: Bearer <accessToken>
와 위의 REST API의 POST
요청을하는
, 당신은 헤더 Authorization: Bearer <accessToken>
와 GET
요청을 통해 아래와 같은 응답 헤더 location
에서 다음 동적 REST API를 얻을 수 있습니다. https://management.azure.com/subscriptions/<subscriptionId>/providers/Microsoft.Network/locations/<region>/operationResults/<objectId, such as f7bfd1fd-e3ea-42f7-9711-44f3229ff877>?api-version=<api-version>
여기 내 예제 코드입니다. 2 단계 동적 REST API를 사용하여
// Get the response header `location` from 1st step REST API
OkHttpClient client = new OkHttpClient();
String url = String.format("https://management.azure.com/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Network/applicationGateways/%s/backendhealth?api-version=%s", subscriptionId, resourceGroupName, appGatewayName, apiVersion);
MediaType JSON = MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, "");
Request request = new Request.Builder().url(url).header("Authorization", "Bearer "+accessToken).post(body).build();
Response response = client.newCall(request).execute();
String location = response.header("Location");
System.out.println(location);
: 1 단계 REST API를 사용
String AUTHORITY = "https://login.windows.net/<tenantId>";
String clientId = "<client-id on management portal(old), or application-id on Azure new portal>";
String clientSecret = "<client-secret-key>";
String subscriptionId = "<subscriptionId>";
String resourceGroupName = "<resource-group-name>";
String appGatewayName = "<applicationgateway-name>";
String apiVersion = "2016-09-01";
// Getting access token
AuthenticationContext context = null;
AuthenticationResult result = null;
ExecutorService service = null;
service = Executors.newFixedThreadPool(1);
context = new AuthenticationContext(AUTHORITY, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", credential, null);
result = future.get();
String accessToken = result.getAccessToken();
System.out.println(accessToken);
// Get the response content as the same as the azure-cli command `azure network applicationgateway backend-health show`
Request request2 = new Request.Builder().url(location).header("Authorization", "Bearer " + accessToken).build();
// Notice for the below code, see under the code
Response response2 = client.newCall(request2).execute();
System.out.println(response2.body().string());
주의 사항 : 내가 POSTMAN
를 통해 콘텐츠를 할 수 있었다 ,하지만 두 번째 단계는 응답 내용 null
을 받았습니다. g OKHttp
/Apache HttpClient
/HttpURLConnection
(자바) &이 무슨 일인지 모르겠지만, Golang/Jquery에서 구현 된 코드조차도 정상적으로 작동합니다. 그것은 자바에서 HTTP 프로토콜 스택의 구현에 의해 발생하는 것으로 보인다.
한편 위의 REST API에 대한 권한 관련 오류 정보가 있으면 https://github.com/JamborYao/ArmManagement을 참조하여 해결하십시오.
희망이 도움이됩니다. 2 단계 문제를 해결할 수 있다면 솔루션을 Google과 공유하십시오. 문제 해결을 위해 계속 노력하겠습니다.
답장을 보내 주신 Peter에게 감사드립니다. 매우 도움이됩니다. –
푸른 색 콘솔에 웹 응용 프로그램을 만든 경우 자바에서이 현상이 발생합니다. 앱이 기본 인 경우 모든 항목이 정상입니다. –
@AurelAvramescu Great !!! 공유해 주셔서 감사합니다. –