首页 > 解决方案 > Microsoft Graph:请求扩展返回 http 400 错误请求

问题描述

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

这是网址:

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();
    }
}

我该如何解决?谢谢!

标签: javahttpmicrosoft-graph-apiwebservice-client

解决方案


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());

或者,您可以根据需要使用图形服务 java api,这将有助于为您抽象所有交互,或者您可以使用任何可用的其余客户端


推荐阅读