如何使用 C# 创建回复 Microsoft.Graph?
How to create a reply using C# Microsoft.Graph?
我正在尝试通过 Outlook 帐户回复电子邮件。但是,当我尝试创建回复(尚未发送)时,出现以下异常:
ServiceException: Code: RequestBroker--ParseUri
Message: Resource not found for the segment '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("this is my reply");
我正在尝试通过 Outlook 帐户回复电子邮件。但是,当我尝试创建回复(尚未发送)时,出现以下异常:
ServiceException: Code: RequestBroker--ParseUri
Message: Resource not found for the segment '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("this is my reply");