office api接口 java,使用Java中的Office 365 REST API构建后台驻留程序或服务应用程序...

I am trying to build a batch job to access Office 365 Mail API. Up on checking the documentation (the concept is very clear), I wasn't able to find a code sample written for Java. I've found this but it relies on the java pom file but I would love to use REST API or Graphi API directly if possible.

Can someone point me on how to get started with building a Daemon service to access Office 365 REST API without users login ?

UPDATE

I've the following code to get the token using AADL library

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

}

}

Here the Unauthorized error from the http.execute method

HttpResponseProxy{HTTP/1.1 401 Unauthorized [Content-Type:

application/json; charset=utf-8, Server: Microsoft-IIS/8.5,

request-id: 49ca360f-ab4b-42d5-a4b0-9676e4244c21, client-request-id:

49ca360f-ab4b-42d5-a4b0-9676e4244c21, x-ms-ags-diagnostic:

{"ServerInfo":{"DataCenter":"West

US","Slice":"SliceA","ScaleUnit":"003","Host":"AGSFE_IN_8","ADSiteName":"WST"}},

X-Powered-By: ASP.NET, Date: Tue, 06 Sep 2016 20:43:24 GMT,

Content-Length: 244] ResponseEntityProxy{[Content-Type:

application/json; charset=utf-8,Content-Length: 244,Chunked: false]}}

eyJ0eXAiOiJKV1QiLCJxcy76FRUlljRV9tb3RXVkpLSHJ3TEJiZF85cyIsImtpZCI6IlliUkFRUlljRV9tb3RXVkpLSHJ3TEJiZF85cyJ9.eyJhdWdfsas32sub2ZmaWNlMzY1LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2YwMjYzMzUzLWFlYjItNGE4YS1iZThhLTc3Mzc3MmE2MGJlMy8iLCJpYXQiOjE0NzMxOTQ4MjIsIm5iZiI6MTQ3MzE5NDgyMiwiZXhwIjoxNDczMTk4NzIyLCJhcHBpZCI6IjhhNjc2ZjJkLWU1M2MtNDViNy05MzhhLTdiOTE1YjVkZTRiNiIsImFwcGlkYWNyIjoiMasdff4577dHMud2luZG93cy5uZXQvZjAyNjMzNTMtYWViMi00YThhLWJlOGEtNzczNzcyYTYwYmUzLyIsIm9pZCI6IjQ3NDhkZDE5LTAxOTUtNDcwOC04MTNkLTQxMTdhNDhlMTdmOCIsInN1YiI6IjQ3NDhkZDE5LTAxOTUtNDcwOC04MTNkLTQxMTdhNDhlMTdmOCIsInRpZCI6ImYwMjYzMzUzLWFlYjItNGE4YS1iZThhLTc3Mzc3MmE2MGJlMyIsInZlciI6IjEuMCJ9.BKt54345DIfv2WWT4pQ--Nuy-0aHkkht4332r7E4d5mP-EAEKmcQe7y0IPjkYGZTNhyNiG2tVAyb56Gcbessdsfewz_BNoAolTVukxttXc-pFY1_Ol5Adc8T5yio43ixfs88mrVRqZEHsb7c-wjO-otBXocZs8waYXdree83g1JtcnULs7bAGp3VBUhMjuJ2u87363Yq3lfse39_Pt6tRw]

(the token is something similar to this, just changed it to something similar for security)

解决方案

Java walkthrough here (though for a web app using auth code flow): https://dev.outlook.com/restapi/tutorial/java

There's also this sample that uses client credential flow:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值