我正在学校开展项目,我们正在使用java
和javafx
,我们需要从Microsoft Graph API中检索邮件。我一直在尝试使用Apache HttpClient检索这些失败。
它返回的只是响应代码200
,但绝对没有实际内容。
String url = "https://graph.microsoft.com/v1.0/me/messages";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
// add request header
request.addHeader("User-Agent", "Mozilla/5.0");
request.addHeader("Accept", "application/json");
request.addHeader("charset", "utf-8");
request.addHeader("Authorization", String.format("Bearer %s", "token"));
HttpResponse response = client.execute(request);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode()); //Response Code : 200
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent())); //No-content, even when returning response code 200????
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(line); //null
这将打印出来:
Response Code : 200
null
关于我将用户发送到https://login.microsoftonline.com/common/oauth2/v2.0/authorize的身份验证 在javafx中使用WebView。在将用户定向到我的假网址后,我向https://login.microsoftonline.com/common/oauth2/v2.0/token发出POST请求以获取该令牌。
WebEngine webEngine = loginView.getEngine(); //the webview variable loginView
webEngine.load("https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=client_id&redirect_uri=http%3A%2F%2Flocalhost:0%2Fauthorize.html&response_type=code&scope=mail.read");
webEngine.getLoadWorker().stateProperty().addListener(
new ChangeListener<Worker.State>() {
@Override
public void changed(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) {
String url = webEngine.getLocation();
System.out.println(url);
String[] paths = url.split("\\?");
if (paths[0].equals("http://localhost:0/authorize.html")) {
try {
String[] para = paths[1].split("\\&");
String[] authcodePara = para[0].split("\\=");
String authcode = authcodePara[1];
String clientId = "client_id";
String clientSecret = "client_secret";
String redirectUrl = "http%3A%2F%2Flocalhost:0%2Fauthorize.html";
String grantType = "authorization_code";
String rawData =
"grant_type=" + grantType
+ "&" + "client_id=" + clientId
+ "&" + "client_secret=" + clientSecret
+ "&" + "redirect_uri=" + redirectUrl
+ "&" + "code=" + authcode;
byte[] postData = rawData.getBytes( StandardCharsets.UTF_8 );
int postDataLength = postData.length;
String request = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
URL urlTest = new URL( request );
HttpURLConnection con = (HttpURLConnection) urlTest.openConnection();
con.setDoOutput(true);
con.setInstanceFollowRedirects(false);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("charset", "utf-8");
con.setRequestProperty("Content-Length", Integer.toString(postDataLength ));
con.setUseCaches(false);
try(DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
wr.write( postData );
}
InputStream _is;
if (con.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
_is = con.getInputStream();
} else {
/* error from server */
_is = con.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(_is));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString()); // prints out the json data with the token.
} catch (ProtocolException ex) {
Logger.getLogger(OutlookWebmailController.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(OutlookWebmailController.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
如果有人可以指出我可能会遇到什么问题的某个方向!
所以我尝试了一个简单的HttpURLConnection。这是我的设置:
String url = "https://graph.microsoft.com/v1.0/me/messages";
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
http_conn.setRequestProperty("Accept", "application/json");
http_conn.setRequestProperty("charset", "utf-8");
http_conn.setRequestProperty("Authorization", String.format("Bearer %s", "token"));
http_conn.connect();
System.out.println(String.valueOf(http_conn.getResponseCode()));
InputStream _is;
if (http_conn.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
_is = http_conn.getInputStream();
} else {
/* error from server */
_is = http_conn.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(_is));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
返回以下内容:
200
<data>
哪个有效! buuuut,为什么不接受Apache HttpClient请求?