2014-11-25 206 views
-1

我正在尝试点击Office 365的'getMessages'api。我在java中尝试了以下url和授权标头。但它给了RESPONSE代码500,这是一个内部服务器错误。但是当我从POSTMAN REST客户端尝试相同的时候,它工作得很好。任何人都可以提出我做错了什么?HTTP GET请求在java中给出500错误

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

继应的请求的输出(邮差REST客户端给出下面的响应,因为它是):

{"@odata.context":"https://outlook.office365.com/api/v1.0/$metadata#Me/Messages","value":[{"@odata.id":"https://outlook.office365.com/api/v1.0/Users('[email protected]')/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":"[email protected]","Name":"Piyusha"}},"Sender":{"EmailAddress":{"Address":"[email protected]","Name":"Piyusha"}},"ToRecipients":[{"EmailAddress":{"Address":"[email protected]","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('[email protected]')/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":"[email protected]","Name":"Piyusha"}},"Sender":{"EmailAddress":{"Address":"[email protected]","Name":"Piyusha"}},"ToRecipients":[{"EmailAddress":{"Address":"[email protected]","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}]} 
+0

我强烈建议您比较两个客户端提交的标头。如果不明确,请在此处发布标题(当然,请不要使用auth标题)。我还建议您使用ADAL库进行身份验证。 – 2014-11-25 16:19:08

回答

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

上面一个班轮代码工作对我来说几乎没有依赖性的变化。 :)