Search code examples
javamicrosoft-graph-apiwebsphere-8

download csv report from msgraph api


Using Java 8, on Websphere 8.x . Trying to grab a audit report from the MS Graph API (/v1.0/reports/getEmailActivityUserDetail(period='D7') end point) to do things like report last user login activity, etc.

Generating the OAuth token, etc. all works fine, and I can grab the 302 redirect URL successfully and that lets me download the CSV report file from the browser, using postman or wget, PHP's file_get_contents etc.

Working from home just using a Java 8 command line app, the following code grabs the file fine. If I copy paste the code to my work machine and call it via Websphere web app, I get an IOException with the message "connection reset" (note - NOT reset by peer!). I've added the SSL certificate for reports.office.com and have added the Apache Commons CSV lib/jar to my build and class paths.

Suggestions on how to further debug for fix this are appreciated.

    String targetUrl = "https://reportsncu.office.com/data/v1.0/download?token=eyJ...*snip*...gFCg";
    try {
        InputStream input = new URL(targetUrl).openStream();
        Reader r = new InputStreamReader(input);
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(r);
        for (CSVRecord record : records) {
            String user = record.get(1);
            String lastActive = record.get(5);
            if (user.equals("user@example.com")) {
                System.out.println(user + " last active " + lastActive);
            }
        }
        r.close();
        input.close();

    } catch (MalformedURLException ex) {
        System.out.println("MalformedURLException: "+ex.getMessage());
    } catch (IOException ex) {
        System.out.println("IOException: "+ex.getMessage());
    }

"Interesting" part of the StackTrace() indicates there may be something SSL going on, but it isn't the usual "forgot to import webserver cert into websphere config" error I usually see when first connecting to an API service, etc.

SystemErr R java.net.SocketException: Connection reset
SystemErr R at java.net.SocketInputStream.read(SocketInputStream.java:220)
SystemErr R at java.net.SocketInputStream.read(SocketInputStream.java:152)
SystemErr R at com.ibm.jsse2.a.a(a.java:79)
SystemErr R at com.ibm.jsse2.a.a(a.java:182)
SystemErr R at com.ibm.jsse2.as.a(as.java:883)
SystemErr R at com.ibm.jsse2.as.i(as.java:969)
SystemErr R at com.ibm.jsse2.as.a(as.java:680)
SystemErr R at com.ibm.jsse2.as.startHandshake(as.java:859)
SystemErr R at com.ibm.ws.ssl.config.WSSocket.startHandshake(WSSocket.java:414)
SystemErr R at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:16)
SystemErr R at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:44)
SystemErr R at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1561)
SystemErr R at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1489)
SystemErr R at com.ibm.net.ssl.www2.protocol.https.b.getInputStream(b.java:93)
SystemErr R at java.net.URL.openStream(URL.java:1057)

Solution

  • Report back if this doesn't help, @ivanivan!

    The stack trace you posted brought back horrible memories of the IBM JDK :) I don't have anything specific that's public - but this help some! https://www.ibm.com/developerworks/community/forums/html/topic?id=ec367703-7957-4249-9f0b-559c717c0c87