Home > Net >  MS Graph SDK retrive mailboxSettings return invalid user error
MS Graph SDK retrive mailboxSettings return invalid user error

Time:01-05

I tried to use MS Graph API to implement a backend API to access other users email setting (for getting out-of-office message). As it is backend API, client credential flow is used. I already granted the permissions "MailboxSettings.Read" and "MailboxSettings.ReadWrite" with application type.

  1. I used my free Azure account for testing. Assume my login account is [email protected], then my Azure domain is testhotmail.onmicrosoft.com. I created one more user [email protected]

  2. I can get the result using Graph Explorer as below

https://graph.microsoft.com/v1.0/users/[email protected]
https://graph.microsoft.com/v1.0/users/[email protected]/mailboxSettings

https://graph.microsoft.com/v1.0/users/[email protected]

But it return error for below using Graph Explorer

{ "error": { "code": "ErrorInvalidUser", "message": "The requested user '[email protected]' is invalid." } }

https://graph.microsoft.com/v1.0/users/[email protected]/mailboxSettings

3a. If call by MS Graph SDK to get the user info for [email protected] as below, it is success

var scopes = new[] { "https://graph.microsoft.com/.default" };
    
var options = new TokenCredentialOptions
{
   AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
    
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    
var user = await graphClient.Users["[email protected]"].Request().GetAsync();

3b. If call by MS Graph SDK to get the user info for [email protected], it returns error

Microsoft.Graph.ServiceException: 'Code: Request_ResourceNotFound Message: Resource '[email protected]' does not exist or one of its queried reference-property objects are not present.

var user = await graphClient.Users["[email protected]"].Request().GetAsync();
  1. If call by MS Graph SDK to get the mailbox setting as below, it returned error

Microsoft.Graph.ServiceException: 'Code: ErrorInvalidUser Message: The requested user '[email protected]' is invalid.

var scopes = new[] { "https://graph.microsoft.com/.default" };
    
var options = new TokenCredentialOptions
{
   AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
    
var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
    
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var mail = await graphClient.Users["[email protected]"].Request().Select("MailboxSettings").GetAsync();

Or returned error for below

Microsoft.Graph.ServiceException: 'Code: ResourceNotFound Message: Resource could not be discovered.

var mail = await graphClient.Users["[email protected]"].Request().Select("MailboxSettings").GetAsync();

CodePudding user response:

using Microsoft.Graph;
using Azure.Identity;

var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = "tenant_name.onmicrosoft.com";
var clientId = "aad_app_id";
var clientSecret = "client_secret";
var clientSecretCredential = new ClientSecretCredential(
                tenantId, clientId, clientSecret);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var user = await graphClient.Users["[email protected]"]
    .Request()
    .Select("MailboxSettings")
    .GetAsync();
var automaticRepliesSetting = user.MailboxSettings.AutomaticRepliesSetting;

Could you pls try this? By the way you may also try to add the 2 application permissions which mentioned in the document: MailboxSettings.Read, MailboxSettings.ReadWrite. And the most important is, your error message is invalid user, so I'm afraid you can use user_PrincipalName instead of [email protected]. You can try to get the user_id in Azure AD potal or from the result for await graphClient.Users["[email protected]"].Request().GetAsync();.

CodePudding user response:

You are using hotmail.com , as per the doc you should also have either a personal Microsoft account with a mailbox on Outlook.com, or a Microsoft work or school account.

Hope this helps

Thanks

  • Related