使用MS Graph SDK获取mailboxSettings时遇无效用户等错误求助
使用MS Graph API实现后端接口,通过客户端凭证流获取其他用户的邮箱设置(用于读取外出消息),已为应用授予MailboxSettings.Read和MailboxSettings.ReadWrite应用类型权限。
测试环境
- 免费Azure账户,登录账户为
test@hotmail.com,Azure域为testhotmail.onmicrosoft.com - 已在Azure中创建用户
client@testhotmail.onmicrosoft.com
Graph Explorer测试结果
成功调用的接口
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
调用失败的接口及错误信息
调用https://graph.microsoft.com/v1.0/users/client@testhotmail.onmicrosoft.com/mailboxSettings返回错误:
{
"error": {
"code": "ErrorInvalidUser",
"message": "The requested user 'client@testhotmail.onmicrosoft.com' is invalid."
} }
MS Graph SDK测试结果
3a. 获取client用户信息成功
执行以下代码可正常获取client@testhotmail.onmicrosoft.com的用户信息:
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. 获取test用户信息失败
执行以下代码获取test@hotmail.com用户信息时返回错误:
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();
获取邮箱设置的SDK调用错误
调用test用户邮箱设置失败
执行以下代码返回错误:
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();
调用client用户邮箱设置失败
执行以下代码返回错误:
Microsoft.Graph.ServiceException: 'Code: ResourceNotFound Message:
Resource could not be discovered.
代码:
var mail = await graphClient.Users["client@testhotmail.onmicrosoft.com"].Request().Select("MailboxSettings").GetAsync();
内容的提问来源于stack exchange,提问作者Sang

