java 通过 microsoft graph 调用outlook(一)
说一句废话。
我曾使用过 microsoft-graph 6.x.x 版本 速度非常慢(请求10条邮件要30-50s之间),不知道为什么 所以改用了microsoft-graph 5.77.0
一 官方文档
先看一下官方文档,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") //应用程序(客户端)ID
.clientSecret("clientSecret") //证书/密钥
.tenantId("tenantId") //租户ID
.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;
}
}
四 结果