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.
I used my free Azure account for testing. Assume my login account is test@hotmail.com , then my Azure domain is testhotmail.onmicrosoft.com. I created one more user client@testhotmail.onmicrosoft.com
I can get the result using Graph Explorer as below
https://graph.microsoft.com/v1.0/users/test@hotmail.com
https://graph.microsoft.com/v1.0/users/test@hotmail.com/mailboxSettings
https://graph.microsoft.com/v1.0/users/client@testhotmail.onmicrosoft.com
But it return error for below using Graph Explorer
{ "error": { "code": "ErrorInvalidUser", "message": "The requested user 'client@testhotmail.onmicrosoft.com' is invalid." } }
https://graph.microsoft.com/v1.0/users/client@testhotmail.onmicrosoft.com/mailboxSettings
3a. If call by MS Graph SDK to get the user info for client@testhotmail.onmicrosoft.com 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["client@testhotmail.onmicrosoft.com"].Request().GetAsync();
3b. If call by MS Graph SDK to get the user info for test@hotmail.com , it returns error
Microsoft.Graph.ServiceException: 'Code: Request_ResourceNotFound Message: Resource 'test@hotmail.com' does not exist or one of its queried reference-property objects are not present.
var user = await graphClient.Users["test@hotmail.com"].Request().GetAsync();
Microsoft.Graph.ServiceException: 'Code: ErrorInvalidUser Message: The requested user 'test@hotmail.com' 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["test@hotmail.com"].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["client@testhotmail.onmicrosoft.com"].Request().Select("MailboxSettings").GetAsync();
You are using hotmail.com, as per thedoc 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
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["xx@xx.onmicrosoft.com"]
.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 myuser@hotmail.com
. You can try to get the user_id
in Azure AD potal or from the result for await graphClient.Users["myuser@hotmail.com"].Request().GetAsync();
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.