gpt4 book ai didi

java - Azure 从 Azure Java SDK 检索虚拟机的 PublicIPAddress

转载 作者:行者123 更新时间:2023-12-01 11:04:04 25 4
gpt4 key购买 nike

作为answer找不到页面,我尝试使用以下代码检索公共(public) IP 的大小

Configuration config = ManagementConfiguration.configure(
new URI(uri),
subscriptionId,
keyStoreLocation, // the file path to the JKS
keyStorePassword, // the password for the JKS
KeyStoreType.jks // flags that I'm using a JKS keystore
);

NetworkResourceProviderClient networkResourceProviderClient = NetworkResourceProviderService.create(config);
PublicIpAddressListResponse PublicIpAddressListResponse =networkResourceProviderClient.getPublicIpAddressesOperations().listAll();
ArrayList<PublicIpAddress> PublicIpAddressList =PublicIpAddressListResponse.getPublicIpAddresses();
System.out.println(PublicIpAddressList.size());

使用 Azure AD ServicePrincipal 身份验证,它返回 - 0

通过"https://management.azure.com/ " API 使用证书身份验证,它返回 - AuthenticationFailed:

Exception in thread "main" com.microsoft.windowsazure.exception.ServiceException: AuthenticationFailed: Authentication failed. The 'Authorization' header is not present or provided in an invalid format.
at com.microsoft.windowsazure.exception.ServiceException.createFromJson(ServiceException.java:290)
at com.microsoft.azure.management.network.PublicIpAddressOperationsImpl.listAll(PublicIpAddressOperationsImpl.java:1443)
at com.microsoft.azure.auth.Program.main(Program.java:50)

知道如何检索所有虚拟机的公共(public) IP 地址吗?或者如何对其进行身份验证以获取 IP 值?

最佳答案

该问题是由于使用不正确的身份验证引起的。

下面的身份验证代码仅适用于 Azure 服务管理。

Configuration config = ManagementConfiguration.configure(
new URI("https://management.core.windows.net),
subscriptionId,
keyStoreLocation, // the file path to the JKS
keyStorePassword, // the password for the JKS
KeyStoreType.jks // flags that I'm using a JKS keystore
);

针对 Azure 资源管理进行身份验证,文档“验证 Azure 资源管理请求”( https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx ) 指出“使用 Azure 资源管理器对资源执行的所有任务都必须使用 Azure Active Directory 进行身份验证。”。

因此,您需要使用您的 subscription-id、tenant-id、client-id 和 client-secret 修改身份验证配置代码,如下所示:

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.windows.net/" + "<your tenant id>",
false, service);
// TODO: add your client id and client secret
ClientCredential cred = new ClientCredential("<your client id>",
"<your client secret>");
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;
}


Configuration config = ManagementConfiguration.configure(
null,
new URI("https://management.core.windows.net),
"<your-subscription-id>",
getAccessTokenFromServicePrincipalCredentials()
.getAccessToken()
);

ServicePrincipal for Java完整认证代码请引用https://github.com/Azure/azure-sdk-for-java/blob/master/azure-mgmt-samples/src/main/java/com/microsoft/azure/samples/authentication/ServicePrincipalExample.java

对于线程( Retrieve List of networks for a subscription in windows azure with azure java sdk )答案的 url,它移动 https://github.com/Azure/azure-sdk-for-java/blob/master/service-management/azure-svc-mgmt-network/src/main/java/com/microsoft/windowsazure/management/network/NetworkOperations.java

关于java - Azure 从 Azure Java SDK 检索虚拟机的 PublicIPAddress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33127506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com