我正在尝试获取用户所属的所有组。当我运行下面的代码时,出现 405 错误。我没有正确调用资源吗?如下:https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations#GetUsersMemberships
@RequestMapping(value="/groups", method = { RequestMethod.GET, RequestMethod.POST })
public JSONArray getMembersOf(HttpServletRequest httpRequest) {
try {
HttpSession session = httpRequest.getSession();
AuthenticationResult result =
(AuthenticationResult) session.getAttribute(AuthHelper.PRINCIPAL_SESSION_NAME);
String accessToken = result.getAccessToken();
String tenant = session.getServletContext().getInitParameter("tenant");
URL url =
new URL(String.format("https://graph.windows.net/%s/users/userId@abc.onmicrosoft.com/getMemberGroups?api-version=2013-04-05",
tenant, accessToken));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// Set the appropriate header fields in the request header.
conn.setRequestProperty("api-version", "2013-04-05");
conn.setRequestProperty("Authorization", accessToken);
conn.setRequestProperty("Accept", "application/json;odata=minimalmetadata");
String goodRespStr = HttpClientHelper.getResponseStringFromConn(conn, true);
System.out.println("REsponse is --------------->>>>> "+goodRespStr);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
错误:
java.io.IOException: Server returned HTTP response code: 405 for URL: https://graph.windows.net/abc.onmicrosoft.com/users/userId@abc.onmicrosoft.com/getMemberGroups?api-version=2013-04-05
最佳答案
根据您的代码,根据我的理解,我认为您想要获取包含用户所属组的对象 ID 的集合
,即 Azure AD REST API Get Member Groups
for Users
。服务器返回的错误代码似乎意味着 Method Not allowed
,您可以引用 HTTP RFC doc 的 10.4.6 405 Method Not Allowed
部分。
我认为该问题可能是由于使用了过时的 api-version
值引起的,请使用 1.6
而不是 2013-04-05
> 在您的代码中然后重试。
如有任何更新,请随时告诉我。
关于java - 从 MS Azure Active Directory 获取用户组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41717261/