简体   繁体   English

GraphAPI 返回“Resource Not Found”试图获取电子邮件列表

[英]GraphAPI returns “Resource Not Found” trying to get email list

GraphAPI allows me to authenticate in my application, but returns Resource could not be discovered when attempting to fetch my email messages, calendar, or anything else. GraphAPI 允许我在我的应用程序中进行身份验证,但在尝试获取我的电子邮件、日历或其他任何内容时返回无法发现资源 After authenticating, the scope returned from the auth request matches the scope I've setup in the Azure portal for the app.身份验证后,从身份验证请求返回的范围与我在 Azure 门户中为应用程序设置的范围相匹配。 My profile info is also correct.我的个人资料信息也是正确的。

I'd greatly appreciate any help with this.我将不胜感激。

Thanks!谢谢!

string scope = "https://graph.microsoft.com/.default";

var builder = PublicClientApplicationBuilder.Create(ClientId)
    .WithAuthority($"{Authority}{Tenant}")
    .WithDefaultRedirectUri();

_clientApp = builder.Build();
TokenCacheHelper.EnableSerialization(PublicClientApp.UserTokenCache);

AuthenticationResult authResult = null;
IAccount firstAccount;

var accounts = await PublicClientApp.GetAccountsAsync();
firstAccount = accounts.FirstOrDefault();

try
{
    authResult = await PublicClientApp.AcquireTokenSilent(scope, firstAccount)
        .ExecuteAsync();
}
catch (MsalUiRequiredException)
{
    try
    {
        authResult = await PublicClientApp.AcquireTokenInteractive(scope)
            .WithAccount(firstAccount)
            .WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
            .ExecuteAsync();
    }
    catch (MsalException msalex)
    {
        Console.WriteLine($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}");
    return false;
}

if (authResult != null)
{
    try
    { 
        var graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                (requestMessage) =>
                {
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authResult.AccessToken);
                    return (Task.FromResult(0));
                }
        ));

        var me = await graphClient.Me.Request().GetAsync();
        mUserName = me.DisplayName;           // This works!

        try
        {
            var messages = await graphClient.Me.Messages
                .Request()                    // This fails
                .GetAsync();
        }
        catch (Exception) {}
    }
}

} }

Thanks for reaching out, have you provisioned a mailbox for that user?感谢您与我们联系,您是否为该用户配置了邮箱? The lack of a mailbox might be why that error is returned when trying to fetch messages.缺少邮箱可能是尝试获取消息时返回该错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM