2

Java SDK を使用して、新しい Azure ポータルで利用可能な IP VM を一覧表示したいと思います。

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

早送りすると、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ファイルの作成方法を見る」リンクに行くように頼まれました

(私のものは Web アプリケーションではなく、AD をネイティブ クライアント アプリケーションとして作成しようとすると、構成タブでキーを保存できないため、Web アプリケーションを作成する必要がありました)

これをすべて行った後、以下のエラーで立ち往生しました

    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 の使用を開始する簡単な方法はありますか?

4

1 に答える 1

1

@Vikram, I suggest that you can try to refer to the article to create an application on AAD.

Then you can follow the code below to get the access token for authentication.

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

If you want to list the VMs on new portal, you can try to use the REST API List the resources in a subscription to get all resources and filter the VMs via the resource type Microsoft.Compute/virtualMachines.

Hope it helps.

于 2016-08-11T09:38:48.757 に答える