2017-07-04 3 views
1

私はAzureClient java sdkを実行しています。私はこのようなkeyvaultクライアントを作成します。Azure KeyVaultErrorException getKey

ApplicationTokenCredentials applicationTokenCredentials=new 
ApplicationTokenCredentials(APPLICATION_ID, "DOMAIN", CLIENT_SECRET, 
AzureEnvironment.AZURE); 
vc = new KeyVaultClient(applicationTokenCredentials); 

と私は紺碧のディレクトリからキーを取得するには、このコードを書く:

Future<KeyBundle> keyBundleFuture = vc.getKeyAsync(testKeyIdentifier, new ServiceCallback<KeyBundle>() { 
    public void failure(Throwable throwable) { 

    } 

    public void success(KeyBundle keyBundle) { 
     System.out.print(keyBundle.toString()); 
    } 
}); 
KeyBundle keyBundle = keyBundleFuture.get(); 

が、私はに

Exception in thread "main" java.util.concurrent.ExecutionException: com.microsoft.azure.keyvault.models.KeyVaultErrorException: Status code 401. 

また、このエラーを取得しています秘密のポータルからキーボームにアクセスするための私の申し込みに許可を与えたことに注意してください。

答えて

2

ステータスコードに従って401のエラーとREST APIリファレンスAuthentication, requests, and responsesのキーボルトは、Azure Java SDKで不正な資格情報を使用したために発生しました。 Azure SDKを使用して鍵ボールトにアクセスするには、doAuthenticateのメソッドを実装する必要があるKeyVaultCredentialsで認証されている必要があります。

参考として、以下に私のサンプルコードを示します。

ServiceClientCredentials credentials = new KeyVaultCredentials() { 

    @Override 
    public String doAuthenticate(String authorization, String resource, String scope) { 
     AuthenticationResult res = null; 

     try { 
      res = GetAccessToken(authorization, resource, clientId, secret); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      return res.getAccessToken(); 
    } 

    private AuthenticationResult GetAccessToken(String authorization, String resource, String clientID, String clientKey) 
      throws InterruptedException, ExecutionException { 
     AuthenticationContext ctx = null; 
     ExecutorService service = Executors.newFixedThreadPool(1); 
     try { 
      ctx = new AuthenticationContext(authorization, false, service); 
     } catch (MalformedURLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Future<AuthenticationResult> resp = ctx.acquireToken(resource, new ClientCredential(
      clientID, clientKey), null); 
      AuthenticationResult res = resp.get(); 
      return res; 
     } 

    }; 
KeyVaultClient client = new KeyVaultClient(credentials); 
String keyIdentifier = "https://<your-keyvault>.vault.azure.net/keys/<your-key>/xxxxxxxxxxxxxxxxxxxxxx"; 
KeyBundle keyBundle = client.getKey(keyIdentifier); 

次に動作します。

+0

ありがとう、それは仕事をしました –

関連する問題