Oauth: Migrate Outlook Web provider to Microsoft Graph API

This commit is contained in:
Ben Olden-Cooligan 2024-07-06 12:44:00 -07:00
parent 2e49ccfa4c
commit 42cb1f5340
2 changed files with 24 additions and 22 deletions

View File

@ -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 }
}
}
}));
}
}

View File

@ -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<string>("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<string>("mail") ?? throw new InvalidOperationException("Could not get Id from Outlook profile response");
}
public async Task<string> 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<string>("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<string>("webLink") ?? throw new InvalidOperationException("Could not get WebLink from Outlook messages response");
}
#endregion