简体   繁体   English

如何使用C#Microsoft.Graph创建回复?

[英]How to create a reply using C# Microsoft.Graph?

I'm trying to reply an email on an outlook account. 我正在尝试在Outlook帐户上回复电子邮件。 But when I try to create the reply (not send it, yet) I get the following exception: 但是,当我尝试创建答复(尚未发送)时,出现以下异常:

ServiceException: Code: RequestBroker--ParseUri ServiceException:代码:RequestBroker--ParseUri

Message: Resource not found for the segment 'microsoft.graph.createReply'. 消息:找不到段“ microsoft.graph.createReply”的资源。

Obviously, the problem is in the Url but doesn't make sense because, when I try to get the message before creating the reply, everything works except when I try to create the reply for that message. 显然,问题出在Url中,但没有意义,因为当我尝试在创建回复之前尝试获取消息时,除我尝试为该消息创建回复外,其他所有操作都有效。

This is the code: 这是代码:

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

I'm using .NET 4.7.2, Microsoft.Graph 1.17, and Microsoft.Identity.Client v3.0.2-preview (this is because if I try to use any stable one I get an error trying to get the token using only the refresh token) 我正在使用.NET 4.7.2,Microsoft.Graph 1.17和Microsoft.Identity.Client v3.0.2-preview(这是因为,如果我尝试使用任何稳定的版本,都会收到错误消息,尝试仅使用刷新令牌)

You're commingling two APIs. 您正在混合两个API。 The Microsoft Graph SDK is designed to work with Microsoft Graph, not the Outlook REST API. Microsoft Graph SDK旨在与Microsoft Graph一起使用,而不是与Outlook REST API一起使用。 While they are nearly identical, you will undoubtedly run into problems (particularly deserializing objects). 尽管它们几乎相同,但是无疑会遇到问题(特别是反序列化对象)。 You should request Graph scopes and the Graph endpoints. 您应该请求图作用域和图端点。

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(); 

Check out this client library: 查看此客户端库:

https://github.com/ivfranji/GraphManagedApi https://github.com/ivfranji/GraphManagedApi

It has method generated on the message to create reply: 它具有在消息上生成的用于创建回复的方法:

Message msg = mailboxBMessages.Items[0]; 消息msg = MailboxBMessages.Items [0];

await msg.Reply("this is my reply"); 等待msg.Reply(“这是我的答复”);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM