我测试代码,但不是Gradle,我的是Maven。我用过
az ad sp create-for-rbac --sdk-auth > my.azureauth
得到
my.azureauth
文件,然后获取
clientID
,
clientScrect
,
SunscribtionID
和
tenantID
. 这是我的结果,效果很好。
这是我的密码。
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.PagedList;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.appservice.WebApp;
public class App
{
public static void main( String[] args )
{
String client="*******";
String tenant="*******";
String key="********";
String subscriptionId="********";
ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(
client, tenant, key, AzureEnvironment.AZURE);
Azure azure = Azure.authenticate(credentials).withSubscription(subscriptionId);
System.out.println("asd");
listWebApps(azure);
System.out.println("asd");
listWebAppsUsingAppServicePlan(azure);
}
private static void listWebAppsUsingAppServicePlan(Azure azure){
PagedList<WebApp> webAppPagedList = azure.appServices().webApps().list();
System.out.printf("There are %d web apps when searched via azure.appServices().webApps()\n", webAppPagedList.size());
for (WebApp app : webAppPagedList) {
System.out.printf("App: %s, Deployment slots: %d", app.name(), app.deploymentSlots().list().size());
}
}
private static void listWebApps(Azure azure){
PagedList<WebApp> webAppPagedList = azure.webApps().list();
System.out.printf("There are %d web apps when searched via azure.webApps()\n", webAppPagedList.size());
for (WebApp app : webAppPagedList) {
System.out.printf("App: %s, Deployment slots: %d", app.name(), app.deploymentSlots().list().size());
}
}
}
注意:在验证和运行获取Web列表的方法时,会花费大量时间。
如果你还有其他问题,请告诉我。