How to update a Mail Message Category. Was getting

2019-07-29 22:19发布

I have seen a few questions posted with this same problem. I think I have all the pieces, but I am still getting the "Empty Payload" error. Does anyone see what is missing?

I want to update the category of some mail messages. I am using the beta endpoint because of this post: Microsoft Graph Client SDK vs. Requesting JSONs

Here is my code:

public async void UpdateMailCategory(GraphServiceClient graphClient, string messageId, string inbox)
{

    string newCategory = @"{'categories': ['Processed']}";

    try
    {
        // Get the request URL for adding a page. 
        string requestUrl = graphClient
            .Users[inbox]
            .Messages[messageId]
            .Request()
            .RequestUrl;

        HttpRequestMessage hrm =
            new HttpRequestMessage(new HttpMethod("PATCH"), requestUrl);
        hrm.Content =
            new StringContent(newCategory, Encoding.UTF8, "application/json");

        // Authenticate (add access token) our HttpRequestMessage
        await graphClient.AuthenticationProvider
            .AuthenticateRequestAsync(hrm);

        // Send the request and get the response.
        HttpResponseMessage response =
            await graphClient.HttpProvider.SendAsync(hrm);
    }
    catch (ServiceException Servex)
    {
        throw Servex;
    }
}

When I look at the hrm.content, it shows this:

{ System.Net.Http.StringContent }
Headers:
{
    Content - Type : application / json;charset = utf - 8
    Content - Length : 35
}

1条回答
别忘想泡老子
2楼-- · 2019-07-29 23:06

You're using the Graph Client SDK is a rather roundabout way which is more likely going to cause you headaches than anything else. It also leads to more complicated code than necessary.

The SDK includes everything you need for the entire request. With the exception of an edge case, you should never need to deal with the HttpProvider or manage HttpRequestMessage and HttpResponseMessage instances.

The following will accomplish the same thing (setting the Categories property of a message) with a lot less complexity:

public async void UpdateMailCategory(GraphServiceClient graphClient, string messageId, string inbox)
{
    try
    {
        await graphClient
            .Users[inbox]
            .Messages[messageId]
            .Request()
            .UpdateAsync(new Message()
            {
                Categories = new List<string>() { "Processed" }
            });
    }
    catch (ServiceException Servex)
    {
        throw Servex;
    }
}

You also shouldn't use the /beta version for this operation as it is supported by the /v1.0 version. You should only need to leverage the /beta version to manage the user's Categories since this isn't generally available yet.

查看更多
登录 后发表回答