【发布时间】:2019-11-20 10:18:39
【问题描述】:
我想授权 Azure Active Directory 授予的 OAuth JSON Web 令牌,我需要做的一件事是使用 Microsoft Graph API 在令牌的 appId 中获取有关应用程序的更多信息。
Microsoft Graph API 允许我通过其 ID 获取应用程序
https://graph.microsoft.com/beta/applications/{id}
,但不是通过它的 appId 通过
https://graph.microsoft.com/beta/applications/{appId}
我认为使用 Microsoft Graph API 获取应用的 AppId 的最佳方式是通过如下过滤器:
https://graph.microsoft.com/beta/applications?filter=appId eq '{appId}'
上述过滤器在 Microsoft Graph Explorer 中运行良好,但是当使用 HttpUrlConnection 使用 GET 请求调用 Graph API 时,我的请求失败并显示 HTTP 代码 400 和消息“Bad Request”。
这很奇怪,因为使用完全相同的 HttpUrlConnection 通过 GET 获取全系列应用程序
https://graph.microsoft.com/beta/applications
工作得很好。
我无法在 Microsoft Graph API GET 请求中使用过滤器功能吗?如何通过 AppId 获取应用信息?
这是我用于 HttpURLConnection 的 Java 代码的 sn-p:
url = new URL(String.format("https://graph.microsoft.com/beta/applications?filter=appId eq '%s'", appId));
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + result.getAccessToken());
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Content-Type", "application/json");
final int httpResponseCode = conn.getResponseCode();
if (httpResponseCode == 200 || httpResponseCode == 201) {
BufferedReader in = null;
final StringBuilder response;
try {
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} finally {
in.close();
}
final JSONObject json = new JSONObject(response.toString());
return json.toString(4);
} else {
return String.format("Connection returned HTTP code: %s with message: %s",
httpResponseCode, conn.getResponseMessage());
}
【问题讨论】:
标签: azure oauth-2.0 httprequest microsoft-graph-api azure-ad-graph-api