2016-08-07 10 views
2

Java SDKを使用して新しいAzureポータルに利用可能なIP VMをリストしたいと思います。azure java sdk authentication

古き良き伝統的なポータルで数年前に、私は通常の管理証明書の手順に従ってvmにアクセスし、vmを作成し、Azureエンドポイントで作業しました。

ここで、新しいポータルと新しいメカニズムを使用してJava SDKとやりとりをしていることがわかりました。私は上記のリンクのどこかで、証明書を使った古い方法ではクラスポータルリソースだけを管理できると読んでいます。

私は、新しいポータルのVMを認証し、リストする簡単なプログラムをコード化しようとしています。彼らはそれをたくさん複雑にしているようだ。

私はその後、私は、このリンクに私は「を参照してくださいどのように行く尋ね

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

を行ってきました「パスワードでサービスプリンシパルを作成する」

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

へ下記のリンクをたどっ上記のページにAuthファイルのリンクを作成してください。

(私はwebappではありません。私はtを作成しようとします。彼は、ネイティブ・クライアント・アプリケーションとしてAD、私がConfigureタブでキーを保存することができていないので、私はこのすべてを行った後、私はこの下のエラー

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) 
で捕まってしまった)

をWebアプリケーションを作成する必要がありました

私がチェックしたときには、Azure Active Directoryに有効なクライアントアプリケーションIDがないためにエラーが発生しているというメッセージが表示されました。

APIの認証と使用を開始する簡単な方法はありますか?

答えて

1

@Vikram私は、articleを参照して、AADでアプリケーションを作成することをお勧めします。

次に、以下のコードに従って認証用のアクセストークンを取得することができます。

// 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(); 

新しいポータル上のVMの一覧を表示したい場合は、すべてのリソースを取得し、リソースタイプMicrosoft.Compute/virtualMachines経由でVMをフィルタリングするためのREST API List the resources in a subscriptionを使用しようとすることができます。

希望します。

関連する問題