从Azure AD Java获取访问令牌
我想通过计费RESTAPI获得Azure RateCard Json响应。 为此,我在eclipse中使用以下代码:从Azure AD Java获取访问令牌,java,eclipse,rest,azure,api,Java,Eclipse,Rest,Azure,Api,我想通过计费RESTAPI获得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; im
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);
}
}
搜索URL时,我会收到以下消息:
AADSTS900561:终结点仅接受POST、OPTIONS和OPTIONS请求。收到GET请求
您需要对AAD中生成的客户端密钥进行URL编码 加 低于
String clientSecret = "<your client secret key generated in AAD>";
你检查过响应体了吗?通常会包含一个详细的错误。对不起,你说的响应体是什么意思?你从康涅狄格州得到的数据。getInputStream@Wuld我建议您可以使用邮递员发送相同的获取访问令牌的请求,以查看有关错误详细信息的响应正文。@Wuld使用https://management.core.windows.net/,它对我也有用。请使用邮递员尝试获取访问令牌以查看响应正文。这是我的样本数字,谢谢!现在GetAccessToken返回一个AccessToken。新错误:服务器返回了URL:/*..*/的HTTP响应代码403。URL给出了消息。授权标头丢失。是否可以创建一个包含更多详细信息的新问题?请添加错误消息详细信息。
clientSecret=java.net.URLEncoder.encode(clientSecret,"UTF-8");
String clientSecret = "<your client secret key generated in AAD>";