From 42cb1f534043bc21cd9ccf522bf52e14f0b23402 Mon Sep 17 00:00:00 2001 From: Ben Olden-Cooligan Date: Sat, 6 Jul 2024 12:44:00 -0700 Subject: [PATCH] Oauth: Migrate Outlook Web provider to Microsoft Graph API --- .../Email/Oauth/OutlookWebEmailProvider.cs | 36 ++++++++++--------- .../Email/Oauth/OutlookWebOauthProvider.cs | 10 +++--- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebEmailProvider.cs b/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebEmailProvider.cs index 5807742ae..23c47b924 100644 --- a/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebEmailProvider.cs +++ b/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebEmailProvider.cs @@ -15,21 +15,21 @@ internal class OutlookWebEmailProvider : IEmailProvider { var messageObj = new JObject { - { "Subject", emailMessage.Subject }, - { "Body", new JObject + ["subject"] = emailMessage.Subject, + ["body"] = new JObject { - { "ContentType", "Text" }, - { "Content", emailMessage.BodyText } - }}, - { "ToRecipients", Recips(emailMessage, EmailRecipientType.To) }, - { "CcRecipients", Recips(emailMessage, EmailRecipientType.Cc) }, - { "BccRecipients", Recips(emailMessage, EmailRecipientType.Bcc) }, - { "Attachments", new JArray(emailMessage.Attachments.Select(attachment => new JObject + ["contentType"] = "text", + ["content"] = emailMessage.BodyText + }, + ["toRecipients"] = Recips(emailMessage, EmailRecipientType.To), + ["ccRecipients"] = Recips(emailMessage, EmailRecipientType.Cc), + ["bccRecipients"] = Recips(emailMessage, EmailRecipientType.Bcc), + ["attachments"] = new JArray(emailMessage.Attachments.Select(attachment => new JObject { - { "@odata.type", "#Microsoft.OutlookServices.FileAttachment" }, - { "Name", attachment.AttachmentName }, - { "ContentBytes", Convert.ToBase64String(File.ReadAllBytes(attachment.FilePath)) } - }))} + ["@odata.type"] = "#microsoft.graph.fileAttachment", + ["name"] = attachment.AttachmentName, + ["contentBytes"] = Convert.ToBase64String(File.ReadAllBytes(attachment.FilePath)) + })) }; var respUrl = await _outlookWebOauthProvider.UploadDraft(messageObj.ToString(), progress); @@ -43,11 +43,13 @@ internal class OutlookWebEmailProvider : IEmailProvider { return new JArray(message.Recipients.Where(recip => recip.Type == type).Select(recip => new JObject { - { "EmailAddress", new JObject { - { "Address", recip.Address }, - { "Name", recip.Name } - }} + "emailAddress", new JObject + { + { "address", recip.Address }, + { "name", recip.Name } + } + } })); } } \ No newline at end of file diff --git a/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebOauthProvider.cs b/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebOauthProvider.cs index c66a076c3..c3bebf1fc 100644 --- a/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebOauthProvider.cs +++ b/NAPS2.Lib/ImportExport/Email/Oauth/OutlookWebOauthProvider.cs @@ -33,7 +33,7 @@ public class OutlookWebOauthProvider : OauthProvider } } - protected override string Scope => "https://outlook.office.com/mail.readwrite https://outlook.office.com/mail.send https://outlook.office.com/user.read offline_access"; + protected override string Scope => "mail.readwrite mail.send user.read offline_access"; protected override string CodeEndpoint => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"; @@ -63,14 +63,14 @@ public class OutlookWebOauthProvider : OauthProvider public string GetEmailAddress() { - var resp = GetAuthorized("https://outlook.office.com/api/v2.0/me"); - return resp.Value("EmailAddress") ?? throw new InvalidOperationException("Could not get Id from Outlook profile response"); + var resp = GetAuthorized("https://graph.microsoft.com/v1.0/me"); + return resp.Value("mail") ?? throw new InvalidOperationException("Could not get Id from Outlook profile response"); } public async Task UploadDraft(string messageRaw, ProgressHandler progress = default) { - var resp = await PostAuthorized("https://outlook.office.com/api/v2.0/me/messages", messageRaw, "application/json", progress); - return resp.Value("WebLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response"); + var resp = await PostAuthorized("https://graph.microsoft.com/v1.0/me/messages", messageRaw, "application/json", progress); + return resp.Value("webLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response"); } #endregion