0

我正在尝试在 Outlook 帐户上回复电子邮件。但是当我尝试创建回复(尚未发送)时,出现以下异常:

ServiceException:代码:RequestBroker--ParseUri

消息:未找到段“microsoft.graph.createReply”的资源。

显然,问题出在 Url 但没有意义,因为当我尝试在创建回复之前获取消息时,除了我尝试为该消息创建回复时,一切正常。

这是代码:

ConfidentialClientApplication cca = new ConfidentialClientApplication(
    [client_id], [redirect_uri],
    new ClientCredential([secret]),
    new TokenCache(), null);

string[] scopes = new string[]
{
    "email",
    "https://outlook.office.com/mail.read",
    "https://outlook.office.com/mail.read.shared",
    "https://outlook.office.com/mail.readwrite",
    "https://outlook.office.com/mail.readwrite.shared",
    "https://outlook.office.com/mail.send",
    "https://outlook.office.com/mail.send.shared"
};

AuthenticationResult result = (cca as IByRefreshToken).AcquireTokenByRefreshTokenAsync(scopes, [refresh_token]).Result;

GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
    requestMessage.Headers.Authorization = new
    AuthenticationHeaderValue("Bearer", result.AccessToken);
    return Task.FromResult(0);
}));

string id = [message_id];
Message details = client
    .Me
    .Messages[id]
    .Request()
    .GetAsync()
    .Result; // JUST FOR TESTING, THIS WORKS!
Message reply = client
    .Me
    .Messages[id]
    .CreateReply()
    .Request()
    .PostAsync().Result; // THIS FAILS!!!

reply.Body.ContentType = BodyType.Html;
reply.Body.Content = [html_code];

client
    .Me
    .Messages[reply.Id]
    .Request()
    .UpdateAsync(reply); // HOPE THIS WORKS

client
    .Me
    .Messages[reply.Id]
    .Send()
    .Request()
    .PostAsync()
    .Wait(); // HOPE THIS WORKS TOO

我正在使用 .NET 4.7.2、Microsoft.Graph 1.17 和 Microsoft.Identity.Client v3.0.2-preview(这是因为如果我尝试使用任何稳定的版本,我在尝试仅使用刷新令牌)

4

2 回答 2

3

您正在混合两个 API。Microsoft Graph SDK 旨在与 Microsoft Graph 一起使用,而不是 Outlook REST API。虽然它们几乎相同,但您无疑会遇到问题(尤其是反序列化对象)。您应该请求 Graph 范围和 Graph 端点。

ConfidentialClientApplication cca = new ConfidentialClientApplication(
    [client_id], [redirect_uri],
    new ClientCredential([secret]),
    new TokenCache(), null);

string[] scopes = new string[]
{
    "mail.read",
    "mail.readwrite",
    "mail.send",
};

AuthenticationResult result = (cca as IByRefreshToken).AcquireTokenByRefreshTokenAsync(scopes, [refresh_token]).Result;

GraphServiceClient client = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) =>
{
    requestMessage.Headers.Authorization = new
    AuthenticationHeaderValue("Bearer", result.AccessToken);
    return Task.FromResult(0);
}));

string id = [message_id];

Message reply = client
    .Me
    .Messages[id]
    .CreateReply()
    .Request()
    .PostAsync().Result; 

client
    .Me
    .Messages[reply.Id]
    .Request()
    .UpdateAsync(new Message() {
        Body = new ItemBody() {
            Content = msg.Body,
            ContentType = BodyType.Html 
        }
     })
    .Wait(); // You have to use a new object and define only the fields you want to change

client
    .Me
    .Messages[reply.Id]
    .Send()
    .Request()
    .PostAsync()
    .Wait(); 
于 2019-08-19T20:50:22.180 回答
0

查看此客户端库:

https://github.com/ivfranji/GraphManagedApi

它具有在消息上生成的方法来创建回复:

消息消息 = 邮箱BMessages.Items[0];

await msg.Reply("这是我的回复");

https://github.com/ivfranji/GraphManagedApi/blob/master/src/Microsoft.Graph.ManagedAPI.FunctionalTests/FunctionalTests/MessageTestDefinition.cs

于 2019-08-21T02:13:04.750 回答