2016-08-07 2 views
2

Java SDK를 사용하여 새로운 Azure 포털에 사용 가능한 IP VM을 나열하고 싶습니다.azure java sdk authentication

몇 년 전에 좋은 고전적인 포털에서 나는 VM에 액세스하고 VM을 생성하고 Azure Endpoints로 작업하기 위해 일반적인 관리 인증서 절차를 따랐습니다.

빠른 속도 이제는 새로운 포털과 새로운 메커니즘을 사용하여 Java SDK와 상호 작용하는 것으로 나타났습니다. 위의 링크에서 어딘가에있는 인증서를 사용하여 클래스 포털 리소스 만 관리 할 수 ​​있다고 읽었습니다.

새로운 포털의 VM을 인증하고 나열하는 간단한 프로그램을 작성하려고합니다. 그들이 그것을 많이 복잡하게하는 것처럼 보입니다.

는 내가 "을 참조하십시오

https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/

가 그럼 난 내가 가서 질문이 링크

https://azure.microsoft.com/en-us/documentation/samples/resources-java-manage-resource-group/

에 갔다"암호 서비스 주체 만들기 "위해 아래 링크를 따라 방법 위의 페이지에서 Auth 파일 만들기 "링크

(내 웹 응용 프로그램이 아니며 t을 만들려고 할 때 그는 네이티브 클라이언트 응용 프로그램으로 AD, 나를 구성 탭 키를 저장할 수 없습니다, 그래서 나는이 모든 일을 후에, 나는이 다음 오류

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for   further details. 
    'authority' Uri should have at least one segment in the path (i.e.https://<host>/<path>/...) 
    java.lang.IllegalArgumentException: 'authority' Uri should have at least one segment in the path (i.e. https://<host>/<path>/...) 
    at com.microsoft.aad.adal4j.AuthenticationAuthority.detectAuthorityType(AuthenticationAuthority.java:190) 
    at com.microsoft.aad.adal4j.AuthenticationAuthority.<init>(AuthenticationAuthority.java:73) 
와 붙어있어)

를 웹 응용 프로그램을 만들 수 있었다

내가 확인했을 때 Azure Active Directory에 유효한 클라이언트 응용 프로그램 ID가 없기 때문에 오류라고합니다.

API를 인증하고 사용하는 간단한 방법이 있습니까?

답변

1

@Vikram, article을 참조하여 AAD에서 응용 프로그램을 만들 수 있습니다.

그런 다음 아래 코드를 따라 인증을위한 액세스 토큰을 얻을 수 있습니다. 새로운 포털에서 VM을 나열 할 경우

// The parameters include clientId, clientSecret, tenantId, subscriptionId and resourceGroupName. 
private static final String clientId = "<client-id>"; 
private static final String clientSecret = "<key>"; 
private static final String tenantId = "<tenant-id>"; 
private static final String subscriptionId = "<subscription-id>"; 

// The function for getting the access token via Class AuthenticationResult 
private static AuthenticationResult getAccessTokenFromServicePrincipalCredentials() 
     throws ServiceUnavailableException, MalformedURLException, ExecutionException, InterruptedException { 
    AuthenticationContext context; 
    AuthenticationResult result = null; 
    ExecutorService service = null; 
    try { 
     service = Executors.newFixedThreadPool(1); 
     // TODO: add your tenant id 
     context = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId, false, service); 
     // TODO: add your client id and client secret 
     ClientCredential cred = new ClientCredential(clientId, clientSecret); 
     Future<AuthenticationResult> future = context.acquireToken("https://management.azure.com/", cred, null); 
     result = future.get(); 
    } finally { 
     service.shutdown(); 
    } 

    if (result == null) { 
     throw new ServiceUnavailableException("authentication result was null"); 
    } 
    return result; 
} 

String accessToken = getAccessTokenFromServicePrincipalCredentials().getAccessToken(); 

, 당신은 모든 자원을 얻을 수있는 REST API를 List the resources in a subscription를 사용하고 자원 유형 Microsoft.Compute/virtualMachines를 통해 VM을 필터링 할 시도 할 수 있습니다.

희망이 있습니다.