廢話不多說
一 官方文檔
先看一下官方文檔,https://learn.microsoft.com/zh-cn/graph/tutorials/java?context=outlook%2Fcontext&tabs=aad&tutorial-step=1
其中的代碼,可以通過地址下載:https://developer.microsoft.com/en-us/graph/quick-start
二 授權方式
microsoft登錄授權方式有很多種,
上面的是OAuth 2.0 授權代碼授予 這個方式
但是這個方式,直接將enter code打印在在控制檯,我沒辦法獲取到(知道的小夥伴可以分享一下)。
因此我換成了 客戶端密碼 方式
三 代碼示例
官方文檔:https://learn.microsoft.com/zh-cn/entra/identity-platform/quickstart-daemon-app-java-acquire-token#register-and-download-your-quickstart-app
1 註冊應用 (官方文檔的step1)https://learn.microsoft.com/zh-cn/graph/tutorials/java?context=outlook%2Fcontext&tabs=aad&tutorial-step=1
2 應用授權
進入應用後,API權限--添加權限--選擇權限(我選了Mail.ReadWrite和User.Read.All)
然後管理員點擊 代表xxx授予管理員同意
3 獲取應用配置信息
獲取祕鑰
證書和密碼--新客戶端密碼--記錄密碼(密碼只顯示一次,請記錄!),
雖然密碼只顯示一次,但我們可以生成新密碼來獲取新的。
獲取信息
4 代碼
pom文件
<!-- office 365 --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>32.1.3-jre</version> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.11.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>com.microsoft.graph</groupId> <artifactId>microsoft-graph</artifactId> <version>5.77.0</version> </dependency> <!-- office 365 --> <dependency> <groupId>com.microsoft.graph</groupId> <artifactId>microsoft-graph-auth</artifactId> <version>0.3.0</version> </dependency> <dependency> <!-- Include the sdk as a dependency --> <groupId>com.microsoft.graph</groupId> <artifactId>microsoft-graph-core</artifactId> <version>2.0.14</version> </dependency>
java代碼
package com.example.test_project.graphtutorial; import com.azure.identity.ClientSecretCredential; import com.azure.identity.ClientSecretCredentialBuilder; import com.microsoft.graph.authentication.IAuthenticationProvider; import com.microsoft.graph.authentication.TokenCredentialAuthProvider; import com.microsoft.graph.models.Message; import com.microsoft.graph.models.User; import com.microsoft.graph.requests.GraphServiceClient; import com.microsoft.graph.requests.MessageCollectionPage; import okhttp3.Request; import java.io.IOException; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Arrays; public class TestGraph { public static void main(String[] args) throws IOException { ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder() .clientId("clientId") .clientSecret("clientSecret") .tenantId("tenantId") .build(); final TokenCredentialAuthProvider tokenCredAuthProvider = new TokenCredentialAuthProvider( Arrays.asList("https://graph.microsoft.com/.default") , clientSecretCredential ); System.out.println("First Step Reached. "); User user = getUser(tokenCredAuthProvider); System.out.println("User : "+user.displayName+" , Email : "+user.mail); MessageCollectionPage mailMessage=getMail(tokenCredAuthProvider); for (Message message: mailMessage.getCurrentPage()) { // System.out.println("Id: " + message.id); System.out.println("Message: " + message.subject); System.out.println(" From: " + message.from.emailAddress.name); System.out.println(" Status: " + (message.isRead ? "Read" : "Unread")); System.out.println(" Received: " + message.receivedDateTime // Values are returned in UTC, convert to local time zone .atZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime() .format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT))); } } public static User getUser(IAuthenticationProvider authProvider) throws IOException { GraphServiceClient<Request> graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient(); User user=graphClient.users("你的郵箱") .buildRequest() .select("displayName,mail,userPrincipalName") .get(); return user; } public static MessageCollectionPage getMail(IAuthenticationProvider authProvider) throws IOException { GraphServiceClient<Request> graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient(); MessageCollectionPage message = graphClient.users("你的郵箱") // .mailFolders("inbox") .messages() .buildRequest() .select("id,from,isRead,receivedDateTime,subject") .top(5) .orderBy("receivedDateTime DESC") .get(); return message; } }
四 結果