亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從 Azure AD Java 獲取訪問令牌

從 Azure AD Java 獲取訪問令牌

慕標5832272 2022-07-20 16:35:23
我想通過 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。

http://img1.sycdn.imooc.com//62d7c1050001c5d109630343.jpg

clientSecret=java.net.URLEncoder.encode(clientSecret,"UTF-8");

下面到

String clientSecret = "<your client secret key generated in AAD>";


查看完整回答
反對 回復 2022-07-20
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號