繁体   English   中英

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

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

我正在尝试在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(这是因为,如果我尝试使用任何稳定的版本,都会收到错误消息,尝试仅使用刷新令牌)

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

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

查看此客户端库:

https://github.com/ivfranji/GraphManagedApi

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

消息msg = MailboxBMessages.Items [0];

等待msg.Reply(“这是我的答复”);

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