Java发送邮件图形API oAuth2

jm81lzqq  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(215)

我正在尝试使用Microsoft Graph API发送电子邮件:https:API?view=graph-rest-1.0&tabs=java#示例-3--创建带有文件附件的消息并发送消息,但我收到以下错误:

Caused by: com.microsoft.graph.http.GraphServiceException: Error code: BadRequest
Error message: /me request is only valid with delegated authentication flow.

我必须如何指定电子邮件地址从?这是我的代码,我得到我的令牌与现代认证,并在我尝试发送电子邮件

final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                    .clientId(clientId)
                    .clientSecret(clientSecret)
                    .tenantId(tenant)
                    .build();

            final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(clientSecretCredential);

            final GraphServiceClient graphClient =
              GraphServiceClient
                .builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .buildClient();

            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 = "meganb@contoso.onmicrosoft.com";
            toRecipients.emailAddress = emailAddress;
            toRecipientsList.add(toRecipients);
            message.toRecipients = toRecipientsList;
            LinkedList<Attachment> attachmentsList = new LinkedList<Attachment>();
            FileAttachment attachments = new FileAttachment();
            attachments.name = "attachment.txt";
            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.me()
                .sendMail(UserSendMailParameterSet
                    .newBuilder()
                    .withMessage(message)
                    .withSaveToSentItems(null)
                    .build())
                .buildRequest()
                .post();
izj3ouym

izj3ouym1#

发生此错误的原因是生成的令牌没有您指定的范围。您甚至可以通过将令牌粘贴到jwt.io上来验证这一点。您需要更改应用程序的权限,以便在生成的令牌中添加范围以使用me终结点。此外,如果不执行此操作,您也将无法命中Users终结点

相关问题