0
votes

I'm trying to get User Mailbox Settings from Microsoft Graph v1.0 with the Ms Graph .Net SDK and with application permissions.

I have the correct permissions (MailboxSettings.Read and User.Read.All) and they are admin consented.

Here is the code for the call:

    var settings = await graphClient.Users[{userId}].Request()
        .Select(e => new
        {
            e.MailboxSettings
        })
        .GetAsync();

When I make the call I get the error:

ErrorAccessDenied, Access is denied. Check credentials and try again.

There is (probably) nothing wrong with my graphClient object as I can successfully call other resources, for instance:

var events = await graphClient.Users[{userId}].Events.Request()

I have tried to make a HTTP request to the API and then I get a successful response. (I'm using the same clientId, clientSecret etc. when I create the graphClient and when I fetch the auth token for the API call).

This is the endpoint I'm calling:

https://graph.microsoft.com/v1.0/users/{userId}/mailboxSettings

Why do I get an error when I use the SDK? Is my request incorrect or is there a bug in the SDK?

2

2 Answers

1
votes

Got it. It's a bug: https://github.com/microsoftgraph/msgraph-sdk-dotnet/issues/538. You need both read and write persmissions in order to read MailboxSettings.

0
votes

You can use this with only mailboxsettings.read.

async Task GetMailboxSettings(GraphServiceClient client)
{
    var mailboxSettingsUrl = client.Users["[email protected]"].RequestUrl + "/mailboxsettings";
    HttpRequestMessage hrm = new HttpRequestMessage(HttpMethod.Get, mailboxSettingsUrl);

    await client.AuthenticationProvider.AuthenticateRequestAsync(hrm);

    HttpResponseMessage response = await client.HttpProvider.SendAsync(hrm);

    if (response.IsSuccessStatusCode)
    {
        // Deserialize into OneNotePage object.
        var content = await response.Content.ReadAsStringAsync();
        MailboxSettings mailboxSettings = client.HttpProvider.Serializer.DeserializeObject<MailboxSettings>(content);
    }
}