【问题标题】:Java requests to Microsoft Graph API perform ok but return 500对 Microsoft Graph API 的 Java 请求执行正常,但返回 500
【发布时间】:2020-10-21 07:14:55
【问题描述】:

我有一个带有 3 种方法的小型 Java 程序:

  • 用于从图形 api 获取令牌以进行身份​​验证承载令牌。没关系,token 没问题,返回 200 http
  • 另外两个用于在 Azure AD 中创建用户并分配许可证。 首先,创建用户。令牌正常,用户已创建,平台上的身份验证工作正常(密码设置正常)。但响应我得到 500 http 即使任务在 AzureAD 中正确完成。如果我在 PowerShell 中发出相同的请求,则没有问题,没有错误。请帮我一个建议。我创建用户的方法是:
public static String createUser(String token) {
        String user = "{\n"
                + "  \"accountEnabled\": true,\n"
                + "  \"displayName\": \"displayName-value\",\n"
                + "  \"mailNickname\": \"mailNickname-value\",\n"
                + "  \"userPrincipalName\": \"upn@microsoft.ro\",\n"
                + "  \"passwordProfile\" : {\n"
                + "    \"forceChangePasswordNextSignIn\": false,\n"
                + "    \"password\": \"Complexity2021\"\n"
                + "  },\n"
                + " \"usageLocation\" : \"RO\""
                + "}";
        HttpURLConnection conn = null;
        URL url = null;
        try {
            url = new URL("https://graph.microsoft.com/beta/users");
            conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", "Bearer " + token);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.connect();
            OutputStream os = conn.getOutputStream();
            os.write(user.getBytes());
            os.flush();
            int status = conn.getResponseCode();
            System.out.println(status); // 500!!!!! AND THE USER is ok in AzureAD
            switch (status) {
                case 200:
                case 201:
                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                    br.close();
                    return sb.toString();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception ex1) {
                    ex1.printStackTrace();
                }
            }
        }
        return null;
    }

【问题讨论】:

    标签: java http azure-active-directory microsoft-graph-api


    【解决方案1】:

    忽略了服务器返回的错误数据 - 但根据文档,它是should explain the cause of the 500 status。建议代码:

            // ...
            System.out.println(status); // 500!!!!! AND THE USER is ok in AzureAD
            
            // always reads server response
            StringBuilder sb = new StringBuilder();
            try (BufferedReader br = 
                    new BufferedReader(new InputStreamReader(conn.getInputStream())) {
                String line;
                while ((line = br.readLine()) != null) {
                      sb.append(line + "\n");
                }
            } // <-- br is automatically closed here!
    
            String output = sb.toString();
            if (status == 200 || status == 201) {
                return output;
            } else {
                // choose a suitable exception, or return null but log error
                throw new FooException("Received error " + status + 
                        " while doing Foo; details follow: " + output);
            }
            // ... 
    

    我还建议用try-with-resources 版本替换所有try { } finally { } 的免费资源——它们更安全(总是关闭获取的资源)并且更具可读性。

    【讨论】:

      【解决方案2】:

      感谢您的回复,我检查了 errorStream 并包含:

      {
        "error": {
          "code": "InternalServerError",
          "message": "The MIME type 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2' requires a '/' character between type and subtype, such as 'text/plain'.",
          "innerError": {
            " 
          }
        }
      }
      

      解决办法是:

      conn.setRequestProperty("Accept", "application/json");
      

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多