我想通過(guò) Billing REST Api 獲得 Azure RateCard Json 響應(yīng)。為此,我在 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;}當(dāng)我搜索 URL 時(shí),我收到以下消息:AADSTS900561:端點(diǎn)只接受 POST、OPTIONS 請(qǐng)求。收到一個(gè) GET 請(qǐng)求。
從 Azure AD Java 獲取訪問(wèn)令牌
慕標(biāo)5832272
2022-07-20 16:35:23