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


    一、背景说明

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

    二、具体实现

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

    1. <dependency>
    2. <groupId>com.microsoft.graphgroupId>
    3. <artifactId>microsoft-graphartifactId>
    4. <version>[5.0,)version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.azuregroupId>
    8. <artifactId>azure-identityartifactId>
    9. <version>[1.3,)version>
    10. dependency>

    2.2 代码片段

    1. public static GraphServiceClient initGraphServiceClient() {
    2. IAuthenticationProvider authProvider = new IAuthenticationProvider() {
    3. @Override
    4. public CompletableFuture getAuthorizationTokenAsync(URL requestUrl) {
    5. CompletableFuture future = new CompletableFuture<>();
    6. future.complete(Your_AccessToken);
    7. return future;
    8. }
    9. };
    10. return GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
    11. }
    三、测试

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

    1. public static String sendMail() {
    2. try {
    3. String mfrom = "xxxx@outlook.com";
    4. String to = "xxxx@outlook.com";
    5. GraphServiceClient graphClient = initGraphServiceClient();
    6. Message message = new Message();
    7. message.subject = "Meet for lunch?";
    8. ItemBody body = new ItemBody();
    9. body.contentType = BodyType.TEXT;
    10. body.content = "The new cafeteria is open.";
    11. message.body = body;
    12. LinkedList toRecipientsList = new LinkedList();
    13. Recipient toRecipients = new Recipient();
    14. EmailAddress emailAddress = new EmailAddress();
    15. emailAddress.address = to;
    16. toRecipients.emailAddress = emailAddress;
    17. toRecipientsList.add(toRecipients);
    18. message.toRecipients = toRecipientsList;
    19. // 构建附件
    20. LinkedList attachmentsList = new LinkedList();
    21. FileAttachment attachments = new FileAttachment();
    22. attachments.name = "hello word.txt";
    23. attachments.oDataType = "#microsoft.graph.fileAttachment";
    24. attachments.contentType = "text/plain";
    25. attachments.contentBytes = Base64.getDecoder().decode("SGVsbG8gV29ybGQh");
    26. attachmentsList.add(attachments);
    27. AttachmentCollectionResponse attachmentCollectionResponse = new AttachmentCollectionResponse();
    28. attachmentCollectionResponse.value = attachmentsList;
    29. AttachmentCollectionPage attachmentCollectionPage = new AttachmentCollectionPage(
    30. attachmentCollectionResponse, null);
    31. message.attachments = attachmentCollectionPage;
    32. // 以指定用户邮箱发送邮件
    33. graphClient.users(mfrom).sendMail(
    34. UserSendMailParameterSet.newBuilder().withMessage(message).withSaveToSentItems(true).build())
    35. .buildRequest().post();
    36. log.info("send email success");
    37. return "success";
    38. } catch (Exception e) {
    39. e.printStackTrace();
    40. log.error("send email error : {}", e.getMessage());
    41. return e.getMessage();
    42. }
    43. }

  • 相关阅读:
    运算放大器基本放大电路
    关于什么是框架
    回坑记之或许是退役赛季?
    关于uniapp H5应用无法在触摸屏正常显示的处理办法
    库兹涅茨周期
    如何快捷地查看H.265视频播放器EasyPlayer的API属性及其使用方法?
    盛元广通农业种子检测实验室信息化管理系统LIMS
    HashMap
    精准DNA甲基化/羟甲基化测序(oxBS-seq)|易基因技术推介
    传智教育 | Git代码冲突-不同分支之间的代码冲突
  • 原文地址:https://blog.csdn.net/gmaaa123/article/details/133979352