Java Azure开发 使用已有token字符串创建GraphServiceClient

本文介绍了如何在Java项目中利用已有的GraphAccessToken创建GraphServiceClient实例,避免使用clientsecret或certificate,并展示了如何使用这个客户端发送带有附件的电子邮件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景说明

在已有的项目中,已经获取到了Graph的AccessToken并保存在内存里面。所以不希望再通过client secret或者certificate去创建GraphServiceClient对象。希望使用现有的token字符串来创建初始化创建GraphServiceClient从而来实现Graph其他API功能。

二、具体实现

2.1 需要Java Graph SDK依赖 加入到pom.xml文件内

 
  <!-- Microsoft Graph SDK for Java -->
	<dependency>
      <groupId>com.microsoft.graph</groupId>
      <artifactId>microsoft-graph</artifactId>
      <version>[5.0,)</version>
  </dependency>
  <!-- Azure AD Authentication SDK for Java -->
  <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-identity</artifactId>
      <version>[1.3,)</version>
  </dependency>

2.2 代码片段

  public static GraphServiceClient initGraphServiceClient() {

		IAuthenticationProvider authProvider = new IAuthenticationProvider() {

			@Override
			public CompletableFuture<String> getAuthorizationTokenAsync(URL requestUrl) {
				CompletableFuture<String> future = new CompletableFuture<>();
				future.complete(Your_AccessToken);
				return future;
			}
		};

		return GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();

	}
三、测试

下面代码是实现调用创建GraphServiceClient,调用graph api发送带附件的邮件。

	public static String sendMail() {

		try {
			String mfrom = "xxxx@outlook.com";
            String to = "xxxx@outlook.com";
			GraphServiceClient<Request> graphClient = initGraphServiceClient();

			Message message = new Message();
			message.subject = "Meet for lunch?";
			ItemBody body = new ItemBody();
			body.contentType = BodyType.TEXT;
			body.content = "The new cafeteria is open.";
			message.body = body;
			LinkedList<Recipient> toRecipientsList = new LinkedList<Recipient>();
			Recipient toRecipients = new Recipient();
			EmailAddress emailAddress = new EmailAddress();
			emailAddress.address = to;
			toRecipients.emailAddress = emailAddress;
			toRecipientsList.add(toRecipients);
			message.toRecipients = toRecipientsList;
			// 构建附件
			LinkedList<Attachment> attachmentsList = new LinkedList<Attachment>();
			FileAttachment attachments = new FileAttachment();
			attachments.name = "hello word.txt";
			attachments.oDataType = "#microsoft.graph.fileAttachment";
			attachments.contentType = "text/plain";
			attachments.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
			attachmentsList.add(attachments);
			AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
			attachmentCollectionResponse.value = attachmentsList;

			AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(
					attachmentCollectionResponse, null);
			message.attachments = attachmentCollectionPage;
			// 以指定用户邮箱发送邮件
			graphClient.users(mfrom).sendMail(
					UserSendMailParameterSet.newBuilder().withMessage(message).withSaveToSentItems(true).build())
					.buildRequest().post();
			log.info("send email success");

			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			log.error("send email error : {}", e.getMessage());
			return e.getMessage();
		}
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值