我试图建立一个批处理作业以访问Office 365邮件API。在检查文档(概念很明确)之前,我找不到用于Java编写的代码示例。我已经找到了它,但是它依赖于java pom文件,但是如果可能的话,我想直接使用REST API或Graphi API。
有人可以指出我如何开始构建守护程序服务以在无需用户登录的情况下访问Office 365 REST API吗?
更新
我有以下代码来使用AADL库获取令牌
String tenant="....";
String authority = "https://login.windows.net/"+tenant+"/oauth2/authorize";
ExecutorService service=null;
service= Executors.newFixedThreadPool(1);
try{
AuthenticationContext authenticationContext= new AuthenticationContext(authority,false,service);
String certFile="/mycert2.pfx";
InputStream pkcs12Cert= new SharedFileInputStream(certFile);
AsymmetricKeyCredential credential=AsymmetricKeyCredential.create("....",pkcs12Cert,"pass");
Future<AuthenticationResult> future=authenticationContext.acquireToken("https://outlook.office365.com",credential,null);
System.out.println("Token Received"+future.get().getAccessToken());
String token = future.get().getAccessToken();
HttpGet httpGet = new HttpGet("https://graph.microsoft.com/v1.0/users");
httpGet.setHeader("Authorization", "Bearer "+token);
GraphServices graphServices = new GraphServices();
ResponseEntity<String> responseEntity;
//responseEntity = graphServices.getEmails(token); //Throws the same Unauthorized exception
HttpClient httpClient= HttpClients.createDefault();
HttpResponse response=httpClient.execute(httpGet);
//response contains Unauthorized access
HttpEntity entity=response.getEntity();
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
}
这是来自http.execute方法的未经授权的错误
HttpResponseProxy {HTTP / 1.1 401未经授权[Content-Type:application / json; charset = utf-8,服务器:Microsoft-IIS / 8.5,请求ID:49ca360f-ab4b-42d5-a4b0-9676e4244c21,客户端请求ID:49ca360f-ab4b-42d5-a4b0-9676e4244c21,x-ms-ags-诊断:{“ ServerInfo”:{“ DataCenter”:“美国西部”,“ Slice”:“ SliceA”,“ ScaleUnit”:“ 003”,“主机”:“ AGSFE_IN_8”,“ ADSiteName”:“ WST”}} ,X-Powered-By:ASP.NET,日期:星期二,2016年9月6日20:43:24 GMT,内容长度:244] ResponseEntityProxy {[Content-Type:application / json; charset = utf-8,Content-Length:244,Chunked:false]}}
eyJ0eXAiOiJKV1QiLCJxcy76FRUlljRV9tb3RXVkpLSHJ3TEJiZF85cyIsImtpZCI6IlliUkFRUlljRV9tb3RXVkpLSHJ3TEJiZF85cyJ9 ..BKt54345DIfv2WWT4pQ - 裸0aHkkht4332r7E4d5mP-EAEKmcQe7y0IPjkYGZTNhyNiG2tVAyb56Gcbessdsfewz_BNoAolTVukxttXc-pFY1_Ol5Adc8T5yio43ixfs88mrVRqZEHsb7c-WJO-otBXocZs8waYXdree83g1JtcnULs7bAGp3VBUhMjuJ2u87363Yq3lfse39_Pt6tRw]
(令牌与此类似,只是出于安全性考虑将其更改为相似的东西)
Java演练此处(尽管对于使用身份验证代码流的Web应用而言):https : //dev.outlook.com/restapi/tutorial/java
还有一个使用客户端凭证流的示例:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句