0

我正在尝试点击 Office 365 的“getMessages”api。我尝试在 java 中跟踪 url 和授权标头。但它给出了响应代码 500,这是一个内部服务器错误。但是当我从 POSTMAN REST CLIENT 尝试相同的操作时,它工作得非常好。谁能建议我做错了什么?

URL url = new URL("https://outlook.office365.com/api/v1.0/me/messages/"); 
        String asB64 = "Basic "+new String(new Base64().encode(user+":"+password.getBytes()));
        println "asB64 : " + asB64;
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        urlConnection.setRequestProperty ("Authorization", asB64);
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoInput(true);
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded,charset=UTF-8");

        PrintWriter printWriter = new PrintWriter(urlConnection.getOutputStream());

        printWriter.flush();

        InputStream inputStream = urlConnection.getInputStream();
        String contentType = urlConnection.getContentType();
        println "output : " + urlConnection.getResponseCode() + " message : " + urlConnection.getResponseMessage() + " error : " + urlConnection.getErrorStream();

以下应该是请求的输出(POSTMAN REST CLIENT 按原样给出以下响应):

{"@odata.context":"https://outlook.office365.com/api/v1.0/$metadata#Me/Messages","value":[{"@odata.id":"https://outlook.office365.com/api/v1.0/Users('test2@lynx.be')/Messages('AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu4AAA=')","@odata.etag":"W/\"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs2\"","Id":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu4AAA=","ChangeKey":"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs2","Categories":[],"DateTimeCreated":"2014-11-25T06:34:35Z","DateTimeLastModified":"2014-11-25T06:34:35Z","Subject":"test me","BodyPreview":"Hello..\r\n\r\n- Piyusha","Body":{"ContentType":"HTML","Content":"\r\n\r\n\r\n\r\n\r\n\r\n\r\n
\r\n
Hello..

\r\n


\r\n
\r\n-         \r\nPiyusha

\r\n


\r\n
\r\n\r\n\r\n"},"Importance":"Normal","HasAttachments":false,"ParentFolderId":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAuAAAAAACFIzMHuQuLQ6LXFS-gt6NSAQBgChXEzbNhRbE9LyPei8StAAAAAAEMAAA=","From":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"Sender":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"ToRecipients":[{"EmailAddress":{"Address":"test2@lynx.be","Name":"Test2"}}],"CcRecipients":[],"BccRecipients":[],"ReplyTo":[],"ConversationId":"AAQkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAQADdgtnhCYExDhAoxDCJGdbk=","DateTimeReceived":"2014-11-25T06:34:35Z","DateTimeSent":"2014-11-25T06:33:57Z","IsDeliveryReceiptRequested":null,"IsReadReceiptRequested":false,"IsDraft":false,"IsRead":false},{"@odata.id":"https://outlook.office365.com/api/v1.0/Users('test2@lynx.be')/Messages('AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu3AAA=')","@odata.etag":"W/\"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs1\"","Id":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOABGAAAAAACFIzMHuQuLQ6LXFS-gt6NSBwBgChXEzbNhRbE9LyPei8StAAAAAAEMAABgChXEzbNhRbE9LyPei8StAAACWUu3AAA=","ChangeKey":"CQAAABYAAABgChXEzbNhRbE9LyPei8StAAACWUs1","Categories":[],"DateTimeCreated":"2014-11-24T05:06:24Z","DateTimeLastModified":"2014-11-24T05:06:24Z","Subject":"Test Email for API","BodyPreview":"Hi,\r\nThis is test email for API testing\r\n\r\n- Piyusha","Body":{"ContentType":"HTML","Content":"\r\n\r\n\r\n\r\n\r\n\r\n\r\n
\r\n


\r\n
Hi,

\r\n
This is test email for API testing

\r\n


\r\n
\r\n-         \r\nPiyusha

\r\n


\r\n
\r\n\r\n\r\n"},"Importance":"Normal","HasAttachments":false,"ParentFolderId":"AAMkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAuAAAAAACFIzMHuQuLQ6LXFS-gt6NSAQBgChXEzbNhRbE9LyPei8StAAAAAAEMAAA=","From":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"Sender":{"EmailAddress":{"Address":"pjadhav@prosperasoft.com","Name":"Piyusha"}},"ToRecipients":[{"EmailAddress":{"Address":"test2@lynx.be","Name":"Test2"}}],"CcRecipients":[],"BccRecipients":[],"ReplyTo":[],"ConversationId":"AAQkADdkMTEyY2RhLTA5YTItNDM0OS05NTVjLWZhNGI2OWUxZjBkOAAQAJE40Qula0HBl35-_7JOrmM=","DateTimeReceived":"2014-11-24T05:06:24Z","DateTimeSent":"2014-11-24T05:05:55Z","IsDeliveryReceiptRequested":null,"IsReadReceiptRequested":false,"IsDraft":false,"IsRead":false}]}
4

1 回答 1

0

我通过使用Unirest for Java解决了这个问题。

我使用了以下代码:

HttpResponse<JsonNode> response = Unirest.get("https://outlook.office365.com/api/v1.0/me/messages").basicAuth(user, password).asJson();

println "response : " + response.getBody();

以上一个班轮代码对我有用,几乎没有依赖项更改。:)

于 2014-11-26T07:10:39.083 回答