Microsoft Graph:请求扩展 returns http 400 错误请求

Microsoft Graph: Requesting an Extension returns http 400 bad request

我在日历中添加了一个事件的开放扩展,并试图读回它。

这里是 url:

https://graph.microsoft.com/v1.0/users/{userid}/calendars/{calendarId}=/events?$expand=Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event')

我无法在 Java 程序中使用它。以下组合有效:

这是我使用 Postman 读取事件时的扩展(在其中一个事件中)。这是活动中的最后一项:

        "extensions@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('{userid}')/calendars('{calendarId}')/events('{eventId})/extensions",
        "extensions": [
            {
                "@odata.type": "#microsoft.graph.openTypeExtension",
                "id": "Microsoft.OutlookServices.OpenTypeExtension.c.i.m.p.server.entities.outlook.Event",
                "extensionName": "c.i.m.p.server.entities.outlook.Event",
                "adherentId": "12346",
                "timeSlotID": "346463"
            }
        ]

这是 Java 代码(Java 8,使用 java.io 和 java.net 库):

    private static void doSomething(String _accessToken) throws IOException {
    String urlString = "https://graph.microsoft.com/v1.0/users/{userId}/calendars/{calendarId}/events?$expand=Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event')";

    URL url = new URL(urlString);

    Proxy webProxy 
  = new Proxy(Proxy.Type.HTTP, new InetSocketAddress({proxy-address}, {port}));
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(webProxy);

    // Set the appropriate header fields in the request header.
    connection.setRequestProperty("Authorization", "Bearer " + _accessToken);
    connection.setRequestProperty("Accept", "application/json");

    connection.setDoOutput(true);
    connection.setReadTimeout(5000);
    connection.setRequestMethod(HttpMethod.GET);

    try {
        connection.connect();
        int responseCode = connection.getResponseCode();
        System.out.println("execute(), response code = " + responseCode);

        String responseMessage = connection.getResponseMessage();
        System.out.println("execute(), response Message = " + responseMessage);

        String responseString = null;
        try {
            InputStream ins = connection.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(ins));
            StringBuffer sb=new StringBuffer();
            String line;
            while ((line=br.readLine()) != null) {
                sb.append(line);
            }
            responseString = sb.toString();
        } catch (Exception e) {
            System.out.println("Could not get input stream from response, error is " + e.toString());
        }

        System.out.println("execute(), httpResult = " + responseString);
    } catch (IOException e) {
        System.out.println(".execute(), IOException : " + e.toString());
    } finally {
        connection.disconnect();
    }
}

我该如何解决这个问题?谢谢!

首先,您应该提供有关错误的更多信息 - Stacktrace 和错误消息。但是 400 代码表示这是用户错误,意味着您发送的请求无效。既然你说邮递员请求有效,那么比较邮递员发送的所有 headers ,看看你的代码是否遗漏了一些听众。至于代码,我建议使用 3d 方 Http 客户端,而不是编写您自己的 Http 客户端功能。这里有一些建议:

  1. Apache Http client - 非常流行和知名的 3d 派对 Http 客户端
  2. OK Http client - Open-source Http client. Here是教程
  3. MgntUtils Http client - 非常简单的3d party HttpClient:在MgntUtils开源库中提供(由我编写)。使用非常简单。看看 Javadoc. Library itself provided as Maven artifacts and on Git(包括源代码和 Javadoc)。

400 表示请求错误。可能是因为 url 编码。 Url 对查询字符串进行编码。

类似

String query = "Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event'";
String url = "https://graph.microsoft.com/v1.0/users/{userId}/calendars/{calendarId}/events?
               $expand=" + URLEncoder.encode(query, StandardCharsets.UTF_8.name());

或者您可以使用 graph service java api based on your need which will help abstract all the interactions for you or you could use any of the rest clients 可用。