英文:
Azure Key Vault error message "AKV10000: Request is missing a Bearer or PoP token."
问题
我在Azure上有一个密钥保管库,并为应用程序注册添加了访问策略,具有秘密权限(获取、列出、设置、删除、恢复、备份、还原)。
我编写了Java代码:
String keyVaultUri = "https://myKeyVault.azure.net";
ClientSecretCredential paramClientSecretCredential = new ClientSecretCredentialBuilder()
.clientId("MyApplicationId")
.clientSecret("MyApplicationSecretKey")
.tenantId("MyTenantId")
.build();
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(paramClientSecretCredential)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
KeyVaultSecret storedSecret = secretClient.getSecret("mySecret");
System.out.println("Secret value: " + storedSecret.getValue());
我使用以下Maven依赖项:
- azure-security-keyvault-secretss(版本4.6.2)
- azure-core(版本1.39.0)
- azure-identity(版本1.9.0-beta.1)
在我启动代码后,我收到了以下错误消息:状态码401,“{"error":{"code":"Unauthorized","message":"AKV10000: Request is missing a Bearer or PoP token."}}”
您有关于如何修复这个问题的建议吗?
英文:
I have Key Vault on Azure and I added Access policies for App registrations with Secret permissions (Get, List, Set, Delete, Recover, Backup, Restore)
I wrote Java code:
String keyVaultUri = "https://myKeyVault.azure.net";
ClientSecretCredential paramClientSecretCredential = new ClientSecretCredentialBuilder()
.clientId("MyApplicationId")
.clientSecret("MyApplicationSecretKey")
.tenantId("MyTenantId")
.build();
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(paramClientSecretCredential)
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.buildClient();
KeyVaultSecret storedSecret = secretClient.getSecret("mySecret");
System.out.println("Secret value: "+storedSecret.getValue());
I'm using these Maven dependencies:
- azure-security-keyvault-secretss (version 4.6.2)
- azure-core (version 1.39.0)
- azure-identity (version 1.9.0-beta.1)
After I started my code I got this error message Status code 401, "{"error":{"code":"Unauthorized","message":"AKV10000: Request is missing a Bearer or PoP token."}}"
Do you have any suggestions on how to fix this?
答案1
得分: 0
我对您的代码进行了一些更改,成功获取了我的密钥保管库的密钥。
代码:
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
public class KeyVaultKam {
public static void main(String[] args) {
String keyVaultUri = "https://<keyvault-name>.vault.azure.net/";
String secretName = "<secrete-name>";
ClientSecretCredential credential = new ClientSecretCredentialBuilder()
.clientId("<client-ID>")
.clientSecret("<client-secrete>")
.tenantId("<tenant-ID>")
.build();
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(credential)
.buildClient();
try {
KeyVaultSecret secret = secretClient.getSecret(secretName);
System.out.println("Secret value: " + secret.getValue());
} catch (Exception e) {
System.out.println("Error retrieving secret: " + e.getMessage());
}
}
}
pom.xml:
我向pom.xml文件添加了以下依赖项:
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.6.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.39.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
我为客户端应用程序创建了如下的访问策略,
输出:
它成功运行并且获取了我的密钥输出如下,
英文:
I made some changes with your code and I got the secrete of my key vault at output.
Code:
import com.azure.identity.ClientSecretCredential;
import com.azure.identity.ClientSecretCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.KeyVaultSecret;
public class KeyVaultKam {
public static void main(String[] args) {
String keyVaultUri = "https://<keyvault-name>.vault.azure.net/";
String secretName = "<secrete-name>";
ClientSecretCredential credential = new ClientSecretCredentialBuilder()
.clientId("<client-ID>")
.clientSecret("<client-secrete>")
.tenantId("<tenant-ID>")
.build();
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(credential)
.buildClient();
try {
KeyVaultSecret secret = secretClient.getSecret(secretName);
System.out.println("Secret value: " + secret.getValue());
} catch (Exception e) {
System.out.println("Error retrieving secret: " + e.getMessage());
}
}
}
pom.xml:
I added below dependencies to the pom.xml file,
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-security-keyvault-secrets</artifactId>
<version>4.6.2</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.39.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.4.0</version>
</dependency>
</dependencies>
I created access policy for the client application as below,
Output:
It runs successfully and got the output of my secrete as below,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论