【问题标题】:Microsoft Graph client error : Authentication challenge is requiredMicrosoft Graph 客户端错误:需要身份验证质询
【发布时间】:2021-03-15 07:26:37
【问题描述】:

我正在尝试使用 Microsoft 在 NuGet 中提供的 Graph SDK 访问 Office 365 用户,我的代码是:

var cca = ConfidentialClientApplicationBuilder
                .Create("ClientId")
                .WithClientSecret("ClientSecret")
                .WithTenantId("TenantId")
                .Build();

List<string> scopess = new List<string>();
scopes.Add("https://graph.microsoft.com/.default");
scopes.Add("Calendars.Read");
// i tried each scope alone and same result
var authenticationProvider = new AuthorizationCodeProvider(cca, scopes.ToArray());
GraphServiceClient graphClient = new GraphServiceClient(authenticationProvider);
var x = await graphClient.Users.Request().GetAsync();

在最后一行我得到了错误

需要身份验证挑战

我的 Azure Active Directory 应用注册设置是:

可能我做错了什么,但不确定是什么! 我使用 Microsoft Docs 作为参考

【问题讨论】:

    标签: azure azure-active-directory microsoft-graph-api office365


    【解决方案1】:

    您正在使用client credential provider

    var authenticationProvider = new AuthorizationCodeProvider(cca, scopes.ToArray()); 替换为ClientCredentialProvider authenticationProvider = new ClientCredentialProvider(cca);

    请注意,您定义了GraphServiceClient graphClient,但您在下一行使用graphClient2graphClient2.Users.Request().GetAsync()。请检查一下。

    【讨论】:

    • 是的,因为在应用注册中定义了 Application 权限,您需要使用客户端凭据流来获取令牌,而不是授权代码流。
    • @maces 如果我的回答对您有帮助,您可以接受它作为答案。谢谢。
    • 我需要等待10分钟才能接受,会这样做
    • 您如何识别正在使用的流?我打算使用授权代码流程,但遇到了 OP 询问的相同错误...
    • @Ortund 请参考:docs.microsoft.com/en-us/graph/sdks/… 检查您是否使用了正确的代码。
    猜你喜欢
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 2022-08-23
    • 2010-11-29
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多