gpt4 book ai didi

java - 如何使用 Java 获取 AzureRateCard?

转载 作者:行者123 更新时间:2023-12-02 10:14:46 25 4
gpt4 key购买 nike

对于我的 Java 程序,我需要 Azure 价目表。为此,Azure 有一个可用于 AzureRateCard 的 API(您可以在此处查看: https://learn.microsoft.com/en-us/azure/cloud-solution-provider/integration/manage-billing/get-azure-prices )。不幸的是,我从未使用过 API,而且我不知道如何处理这些信息。

我希望我能找到已经使用过这个 API 的人 :D谢谢!

最佳答案

我有一个示例代码,可通过计费 REST API 获取 Azure RateCard Json 响应 Get price and metadata information for resources used in an Azure subscription ,而不是 SDK for Java,因为我不知道 SDK 在哪里。对我来说效果很好。

这是我的示例代码。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class RateCardRest {

public static String getAccessToken(String tenantId, String clientId, String clientSecret)
throws MalformedURLException, IOException {
String endpoint = String.format("https://login.microsoftonline.com/%s/oauth2/token", tenantId);
String postBody = String.format("grant_type=client_credentials&client_id=%s&client_secret=%s&resource=%s",
clientId, clientSecret, "https://management.azure.com/");
HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
conn.setRequestMethod("POST");
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
conn.getOutputStream().write(postBody.getBytes());
conn.connect();
// If you want to see the response content, please use the commented code below.
// BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
// StringBuilder builder = new StringBuilder();
// String line = null;
// while ((line = reader.readLine()) != null) {
// builder.append(line);
// }
// reader.close();
// System.out.println(builder.toString());
// The output for access token is {"token_type":"Bearer","expires_in":"3600","ext_expires_in":"3600","expires_on":"1550660092","not_before":"1550656192","resource":"https://management.azure.com/","access_token":"eyJ0eXAiOiJKV1QiL...."}
JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createParser(conn.getInputStream());
String accessToken = null;
while (parser.nextToken() != JsonToken.END_OBJECT) {
String name = parser.getCurrentName();
if ("access_token".equals(name)) {
parser.nextToken();
accessToken = parser.getText();
}
}
return accessToken;
}

public static String getRateCard(String subscriptionId, String apiVersion, String offerId, String currency,
String locale, String region, String accessToken) throws MalformedURLException, IOException {
String endpoint = String.format(
"https://management.azure.com/subscriptions/%s/providers/Microsoft.Commerce/RateCard?api-version=%s&$filter=OfferDurableId eq '%s' and Currency eq '%s' and Locale eq '%s' and RegionInfo eq '%s'",
subscriptionId, apiVersion, offerId, currency, locale, region).replaceAll(" ", "%20");
HttpURLConnection conn = (HttpURLConnection) new URL(endpoint).openConnection();
conn.setRequestMethod("GET");
conn.addRequestProperty("Authorization", "Bearer " + accessToken);
conn.addRequestProperty("Content-Type", "application/json");
conn.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
reader.close();
return builder.toString();
}

public static void main(String[] args) throws MalformedURLException, IOException {
String tenantId = "<your tenant id like xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx";
String clientId = "<your client id registed in AAD like xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx";
String clientSecret = "<your client secret key generated in AAD>";
String accessToken = getAccessToken(tenantId, clientId, clientSecret);
System.out.println(accessToken);
String subscriptionId = "<your subscription id like xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx";
String apiVersion = "2015-06-01-preview";
String offerId = "<your offer id like XX-AZR-XXXXX";
String currency = "USD";
String locale = "en-US";
String region = "US";
String rateCardResp = getRateCard(subscriptionId, apiVersion, offerId, currency, locale, region, accessToken);
System.out.println(rateCardResp);
}

}

注意:rateCardResp内容大小约为7MB,因此下载会花费更多时间。请将其存储到文件作为数据缓存。

注意:要使用下面的代码,您必须在 Azure 门户上收集这些参数,如下所示。

  1. 按照 Register your client application with Azure AD 部分操作引用官方文档 Azure REST API Reference 注册应用程序以获取 clientIdclientSecret 并授予 Owner角色及其在 Azure AD 中的应用程序所需的权限。

enter image description here

enter image description here

enter image description here

  • 转到您的订阅以获取订阅 ID 和优惠 ID。
  • enter image description here

    如果您有任何疑问,请随时告诉我,我将详细更新我的帖子。

    关于java - 如何使用 Java 获取 AzureRateCard?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54760786/

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