提前谢谢。我正试图通过编程方式访问每个azure订阅的成本,以便在应用程序中表示它。我使用了下面的代码,但它提供了这个netty错误,它似乎处理无效的访问。你以前解决过这个问题吗?我有三个假设:
正如我在1中发现的那样,
。
import com.azure.core.credential.TokenCredential;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.management.AzureEnvironment;
import com.azure.core.management.Region;
import com.azure.core.management.profile.AzureProfile;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.core.util.Context;
import com.azure.resourcemanager.billing.BillingManager;
import com.azure.resourcemanager.costmanagement.CostManagementManager;
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.OperatorType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
// Sample to query in a table
public class Query {
public static void main(String[] args) {
AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
TokenCredential credential = new DefaultAzureCredentialBuilder()
.authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
.build();
// System.out.println(profile.getTenantId());
CostManagementManager manager = CostManagementManager
.authenticate(credential, profile);
System.out.println(System.getenv("AZURE_TENANT_ID"));
System.out.println(System.getenv("AZURE_CLIENT_ID"));
System.out.println(System.getenv("AZURE_CLIENT_SECRET"));
try{
customerQueryGroupingModern(manager);
}
catch (final Exception e) {
System.out.println(e);
}
}
//https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/costmanagement/azure-resourcemanager-costmanagement/src/samples/java/com/azure/resourcemanager/costmanagement/QueryUsageSamples.java
/**
* Sample code: CustomerQueryGrouping-Modern.
*
* @param costManagementManager Entry point to CostManagementManager.
*/
public static void customerQueryGroupingModern(
com.azure.resourcemanager.costmanagement.CostManagementManager costManagementManager) {
costManagementManager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/xxxxxxxx/customers/5678",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}发布于 2022-08-30 09:31:08
我在google中寻找类似的错误,据说这个问题与一个不包括在内的netty包有关。在自述中,它被描述为使用netty进行身份验证,但没有编写包。最后,我在sdk的资源管理器中找到了一个,并将其包含在pom.xml中:
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.12.4</version>
</dependency>https://stackoverflow.com/questions/72826560
复制相似问题