当前位置: 首页 > news >正文

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

一、背景说明

在已有的项目中,已经获取到了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() {@Overridepublic 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();}}

http://www.lryc.cn/news/204041.html

相关文章:

  • 【Qt】消息机制和事件
  • 爬虫模拟用户登录
  • asp.net企业招聘管理系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio计算机毕业设计
  • 艾泊宇产品战略:假如中国禁止直播带货,网红和店家该如何应对?
  • C#调用C/C++从零深入讲解
  • MyBatis篇---第五篇
  • 十三水中各种牌型判断LUA版
  • 2023.10.19 关于设计模式 —— 单例模式
  • 4个鲜为人知的Python迭代过滤函数
  • 使用logger.error(“自定义错误信息描述“,e)将错误信息输出到日志文件上
  • 音乐的数字未来:虚拟演唱会与TikTok的巅峰融合
  • 基于图像识别的跌倒检测算法 计算机竞赛
  • NSS [SWPUCTF 2021 新生赛]PseudoProtocols
  • 字节码进阶之JVM Attach API详解
  • Kubernetes 部署 kubeflow1.6.1
  • 设计模式:建造者模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
  • Maxon Cinema 4D 2024:打造独一无二的视觉效果 模拟模块大更新
  • 16.2 ARP 主机探测技术
  • 三级等保-linux服务器三权分立设置
  • 抓取网页的含义和URL基本构成
  • 计算机毕业设计 机器学习深度学习人工智能
  • 施密特正交化
  • 低代码开发:加速应用开发的利器
  • 数据安全发展趋势与密码保护技术研究
  • 368周赛leetcode
  • Vue 的 nextTick:深入理解异步更新机制
  • SQL关于日期的计算合集
  • shell_44.Linux使用 getopt 命令
  • Linux备份Docker的mysql数据并传输到其他服务器保证数据级容灾
  • 【vue+nestjs】qq第三方授权登录【超详细】