[英]Building Daemon or Service Apps with Office 365 REST API in Java
我試圖建立一個批處理作業以訪問Office 365郵件API。 在檢查文檔 (概念很明確)之前,我找不到用於Java編寫的代碼示例。 我已經找到了,但是它依賴於java pom文件,但是如果可能的話,我想直接使用REST API或Graphi API。
有人可以指出我如何開始構建后台駐留程序服務以在無需用戶登錄的情況下訪問Office 365 REST API嗎?
UPDATE
我有以下代碼使用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,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..BKt54345DIfv2WWT4pQ - Nuy-0aHkkht4332r7E4d5mP-EAEKmcQe7y0IPjkYGZTNhyNiG2tVAyb56Gcbessdsfewz_BNoAolTVukxttXc-pFY1_Ol5Adc8T5yio43ixfs88mrVRqZEHsb7c-WJO-otBXocZs8waYXdree83g1JtcnULs7bAGp3VBUhMjuJ2u87363Yq3lfse39_Pt6tRw]
(令牌與此類似,只是出於安全性考慮將其更改為相似的東西)
此處的Java演練(盡管對於使用身份驗證代碼流的Web應用程序而言): https : //dev.outlook.com/restapi/tutorial/java
還有一個使用客戶端憑證流的示例:
要在守護程序或服務應用程序中進行身份驗證,我們可以使用客戶端憑據流。 注冊該應用程序后,我們將獲得秘密 。 然后,我們可以直接使用下面的請求來獲取僅應用訪問令牌:
POST https://login.microsoftonline.com/<tenantId>/oauth2/token HTTP/1.1
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials
&client_id=<clientId>
&client_secret=<clientSecret>
&resource=https://graph.microsoft.com
要使用Office 365訪問令牌,我們可以將資源替換為https://outlook.office.com
。 這是有關在服務或守護程序應用程序中調用Microsoft Graph的詳細文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.