我正在尝试连接到 Office 365 以使用客户端凭据流。我已按照http://blogs.msdn.com/b/exchangedev/archive/2015/01/21/building-demon-中提到的所有步骤进行操作or-service-apps-with-office-365-mail-calendar-and-contacts-apis-oauth2-client-credential-flow.aspx
我正在尝试使用 ADAL java 库进行连接。
使用以下代码连接和获取邮件:
String authority = "https://login.windows.net/tenant-id/oauth2/authorize";
ExecutorService service = null;
service=Executors.newFixedThreadPool(1);
try {
AuthenticationContext authenticationContext = new AuthenticationContext(authority, false, service);
String certfile = "PfxFinal.pfx";
InputStream pkcs12Certificate=new FileInputStream(certfile);
String token = "";
AsymmetricKeyCredential credential = AsymmetricKeyCredential.create("clientid", pkcs12Certificate,"password");
System.out.println("X509 is fine!");
Future<AuthenticationResult> future=authenticationContext.acquireToken("https://outlook.office365.com", (AsymmetricKeyCredential)credential, null);// authenticationContext.acquireToken("https://outlook.office365.com", credential, null);
System.out.println("Token Received "+future.get().getAccessToken());
token=future.get().getAccessToken();
System.out.println(token);
URL url = new URL("https://outlook.office365.com/api/v1.0/me/folders/inbox/messages?$count=true&$filter=isread%20eq%20false");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept","application/json");
//con.setRequestProperty("Authorization",token);
con.setRequestProperty("Authorization","Bearer "+token);
System.out.println("Bearer "+token);
if (con.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ con.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(con.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
con.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我已为租户提供了完全许可。为了解决此问题,我还需要做些什么。