【问题标题】:Get user groups from MS Azure Active Directory从 MS Azure Active Directory 获取用户组
【发布时间】:2017-01-18 10:44:21
【问题描述】:

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

【问题讨论】:

  • 如果accessToken的内容只是JWT,那么授权头应该是:conn.setRequestProperty("Authorization", "Bearer " + accessToken);
  • 我也不确定为什么访问令牌在这个 String.format 调用中:String.format("https://graph.windows.net/%s/users/userId@abc.onmicrosoft.com/getMemberGroups?api-version=2013-04-05", tenant, accessToken)。不应该是:String.format("https://graph.windows.net/%s/users/userId@abc.onmicrosoft.com/getMemberGroups?api-version=2013-04-05", tenant)
  • 我搞定了。实际上我没有使用正确的uri。似乎 2013-04-05 版本有“memberOf”而不是“getMemberGroups”。现在工作正常。谢谢!!
  • 实际上我采用了示例代码 (docs.microsoft.com/en-us/azure/active-directory/…) 并试图使其工作。似乎“String.format”在那里有错字。谢谢,会更正。

标签: java azure active-directory


【解决方案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 并重试。

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

【讨论】:

  • 我已经解决了,如之前的评论中所述。 “getMemberGroups”在 1.6 中不起作用。后来我将“memberOf”与“2013-04-05”一起使用,这有帮助。
猜你喜欢
  • 2018-06-26
  • 1970-01-01
  • 2023-01-26
  • 2018-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多