从MS Azure Active Directory获取用户组

时间:2017-01-18 10:44:22

标签: java azure active-directory

我正在尝试获取用户所属的所有组。当我运行以下代码时,我得到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

1 个答案:

答案 0 :(得分:1)

根据您的代码,根据我的理解,我认为您希望get collection that contains the object IDs of the groups of which the user is a member是Azure AD REST API Get Member Groups for Users。从服务器返回的错误代码似乎意味着Method Not Allowed,您可以参考HTTP RFC doc10.4.6 405 Method Not Allowed部分。

我认为问题可能是由于使用过时的api-version值造成的,请在您的代码中使用1.6 2013-04-05的{​​{1}},然后重试。

任何更新,请随时告诉我。