1

I added an open extension to an event in a calendar and am trying to read it back.

Here is the 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')

I cannot get this to work in a Java program. The following combinations do work:

  • It works my Java program if I remove the $expand... parameter. I can also ask for certain fields, that works too.
  • The request works in Postman (I just have to set the token)
  • The request works in Graph Explorer when I log in as the owner of the calendar

Here is the extension (inside one of the events) when I use Postman to read the event. It is the last item in the event:

        "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"
            }
        ]

Here is the Java code (Java 8, using java.io and java.net libraries):

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

How do I fix this? Thanks!

ssimm
  • 1,908
  • 3
  • 16
  • 36
  • Have you tried encoding the query string ? Something like `String urlString = "https://graph.microsoft.com/v1.0/users/{userId}/calendars/{calendarId}/events?"+ URLEncoder.encode("$expand", "UTF-8") + "="+ URLEncoder.encode("Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event')", "UTF-8")?` – s7vr Sep 17 '19 at 04:05
  • Could you give this url a try ? `https://graph.microsoft.com/v1.0/users/{userid}/calendars/{calendarId}=/events?$expand=Extensions($filter=Id%20eq%20'c.i.m.p.server.entities.outlook.Event')` – s7vr Sep 20 '19 at 14:04
  • @user2683814 The first comment corrected the problem. Actually I didn't have to encode "$expand", just the part at the end. Make it an answer and the bounty is yours. – ssimm Sep 20 '19 at 15:46

2 Answers2

1

400 means bad request. It could be because of url encoding. Url encode the query string.

Something like

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

Alternatively you could use 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 available.

s7vr
  • 73,656
  • 11
  • 106
  • 127
  • I looked at the graph services api but I couldn't see how to work in a proxy. So I gave up and went native. I guess that should be another question. – ssimm Sep 20 '19 at 16:28
0

First of all, you should provide more info on the error - Stacktrace and error message. But 400 code indicates that was a user mistake, meaning that you are sending an invalid request. Since you say that postman request works then compare all the headers that are sent by postman and see if your code misses some hearer. As for the code, instead of coding your own Http client functionality I would suggest using 3d party Http client. Here are a few suggestions:

  1. Apache Http client - very popular and well known 3d party Http Client
  2. OK Http client - Open-source Http client. Here is tutorial
  3. MgntUtils Http client - very simple 3d party HttpClient: Provided in MgntUtils Open source library (written by me). Very simple in use. Take a look at Javadoc. Library itself provided as Maven artifacts and on Git (including source code and Javadoc).
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Ok, I'll look at the headers. But if my code has a header problem then why does the first combination succeed? – ssimm Sep 16 '19 at 20:15
  • The first combination working is an interesting point. But at the moment I would keep it aside and concentrate on the comparison of the same request working in postman and not working from java code. Finding a difference there is the main key point in my opinion – Michael Gantman Sep 18 '19 at 08:29