我想通过 Billing REST Api 获得 Azure RateCard Json 响应。为此,我在 Eclipse 中使用以下代码: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 String name = parser.getCurrentName(); if ("access_token".equals(name)) { parser.nextToken(); accessToken = parser.getText(); } } return accessToken;}当我搜索 URL 时,我收到以下消息:AADSTS900561:端点只接受 POST、OPTIONS 请求。收到一个 GET 请求。
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
您需要对在 AAD 中生成的客户端密钥进行 UrlEncode。
clientSecret=java.net.URLEncoder.encode(clientSecret,"UTF-8");
下面到
String clientSecret = "<your client secret key generated in AAD>";
添加回答
举报
0/150
提交
取消