Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java获得AzureRateCard?_Java_Azure_Api - Fatal编程技术网

如何使用Java获得AzureRateCard?

如何使用Java获得AzureRateCard?,java,azure,api,Java,Azure,Api,对于我的Java程序,我需要一个Azure价格表。对于此Azure,有一个API可用于AzureRateCard(您可以在此处看到:)。不幸的是,我从未使用过API,我不知道如何处理这些信息 我希望我能找到已经使用过这个API的人:D 谢谢大家! 我有一个示例代码,可以通过计费RESTAPI而不是SDK for Java获得Azure RateCard Json响应,因为我不知道SDK在哪里。这对我来说很好 这是我的示例代码 import java.io.BufferedReader; impo

对于我的Java程序,我需要一个Azure价格表。对于此Azure,有一个API可用于AzureRateCard(您可以在此处看到:)。不幸的是,我从未使用过API,我不知道如何处理这些信息

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

我有一个示例代码,可以通过计费RESTAPI而不是SDK for Java获得Azure RateCard Json响应,因为我不知道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);
    }

}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入com.fasterxml.jackson.core.JsonFactory;
导入com.fasterxml.jackson.core.JsonParser;
导入com.fasterxml.jackson.core.JsonToken;
公共类收费卡{
公共静态字符串getAccessToken(字符串tenantId、字符串clientId、字符串clientSecret)
引发畸形的DurLexException,IOException{
字符串端点=字符串。格式(“https://login.microsoftonline.com/%s/oauth2/token“,租户(TID);
String postBody=String.format(“授权类型=客户端凭据&客户端id=%s&客户端机密=%s&资源=%s”,
clientId,clientSecret,“https://management.azure.com/");
HttpURLConnection conn=(HttpURLConnection)新URL(端点).openConnection();
conn.setRequestMethod(“POST”);
conn.addRequestProperty(“内容类型”、“应用程序/x-www-form-urlencoded”);
连接设置输出(真);
conn.getOutputStream().write(postBody.getBytes());
连接();
//如果您想查看响应内容,请使用下面的注释代码。
//BufferedReader=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
//StringBuilder=新的StringBuilder();
//字符串行=null;
//而((line=reader.readLine())!=null){
//builder.append(行);
//      }
//reader.close();
//System.out.println(builder.toString());
//访问令牌的输出为{“令牌类型”:“承载”、“到期日”:“3600”、“外部到期日”:“3600”、“到期日”:“1550660092”、“非之前”:“1550656192”、“资源”:https://management.azure.com/,“访问令牌”:“EYJ0EXAIIOIJKV1QIL…”
JsonFactory工厂=新的JsonFactory();
JsonParser=factory.createParser(conn.getInputStream());
字符串accessToken=null;
while(parser.nextToken()!=JsonToken.END\u对象){
String name=parser.getCurrentName();
if(“访问令牌”。等于(名称)){
parser.nextToken();
accessToken=parser.getText();
}
}
返回accessToken;
}
公共静态字符串getRateCard(字符串subscriptionId、字符串apiVersion、字符串offerId、字符串currency、,
字符串区域设置、字符串区域、字符串accessToken)引发错误的DurLexException、IOException{
字符串端点=String.format(
"https://management.azure.com/subscriptions/%s/providers/Microsoft.Commerce/RateCard?api-版本=%s&$filter=提供的可用ID等式'%s'和货币等式'%s'以及区域设置等式'%s'和区域信息等式'%s',
subscriptionId、apiVersion、offerId、货币、地区、地区)。replaceAll(“,“%20”);
HttpURLConnection conn=(HttpURLConnection)新URL(端点).openConnection();
conn.setRequestMethod(“GET”);
conn.addRequestProperty(“授权”、“持有人”+访问令牌);
conn.addRequestProperty(“内容类型”、“应用程序/json”);
连接();
BufferedReader=新的BufferedReader(新的InputStreamReader(conn.getInputStream());
StringBuilder=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
builder.append(行);
}
reader.close();
返回builder.toString();
}
publicstaticvoidmain(字符串[]args)引发畸形的DurLexException、IOException{

字符串tenantId="到目前为止,您尝试了什么?遇到了任何问题吗?我必须初始化变量IPartner,但我不知道如何初始化嗨,谢谢您的回答,很抱歉我以前没有写过,但我花了一段时间才获得所有这些参数。我需要哪个订阅,每次使用此代码时我都必须支付吗?现在我有了所有参数,并且当我运行a时应用程序我收到以下错误:线程“main”中出现异常java.io.IOException:服务器返回了URL:;;的HTTP响应代码:401;通过搜索URL,我得到以下错误:AADSTS900561:端点仅接受POST、OPTIONS请求。收到get请求。