Microsoft graph api 从MS在示例应用程序中创建GraphServiceClient时出错

Microsoft graph api 从MS在示例应用程序中创建GraphServiceClient时出错,microsoft-graph-api,Microsoft Graph Api,我正在尝试让MS示例应用程序为Graph Beta Webhooks API工作,它目前正在崩溃,因为我不得不修改一些示例代码以删除一些过时的代码,我不确定用什么替换它。 这就是功能: public static GraphServiceClient GetAuthenticatedClient(string userId, string redirect) { var graphClient = new GraphServiceClient(

我正在尝试让MS示例应用程序为Graph Beta Webhooks API工作,它目前正在崩溃,因为我不得不修改一些示例代码以删除一些过时的代码,我不确定用什么替换它。 这就是功能:

    public static GraphServiceClient GetAuthenticatedClient(string userId, string redirect)
    {
        var graphClient = new GraphServiceClient(
            new DelegateAuthenticationProvider(
                async (request) =>
                {
                    var tokenCache = new SampleTokenCache(userId);

                    // Obsolete code
                    //var cca = new ConfidentialClientApplication(Startup.ClientId, redirect, new ClientCredential(Startup.ClientSecret), tokenCache.GetMsalCacheInstance(), null);
                    // New code
                    var cca2 = ConfidentialClientApplicationBuilder.Create(Startup.ClientId).WithClientSecret(Startup.ClientSecret).WithRedirectUri(redirect).Build();
                    // Question - how do I pass the tokenCache in here as the userTokenCache ?

                    // ERROR - With the new code this returns zero accounts presuambly because I haven't passed in a userTokenCache
                    var accounts = await cca2.GetAccountsAsync();

                    // Obsolete code
                    //var authResult = await cca.AcquireTokenSilentAsync(Startup.Scopes, accounts.First());
                    // New code
                    var authResult2 = await cca2.AcquireTokenSilent(Startup.Scopes, accounts.First()).ExecuteAsync();

                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authResult2.AccessToken);
                }));

        return graphClient;
    }
如果我使用SecretentialClientApplicationBuilder,那么GetAccountsAsync()返回一个空集合,我认为这是因为我没有将令牌缓存传递到生成器中。是否有人知道如何修复此代码,或者是否有人让示例应用程序正常工作? 这是指向示例应用程序的链接:

谢谢
Ed James

您需要这样做,以便将帐户添加到令牌缓存中,该缓存可以调用tokenCache.GetMsalCacheInstance()方法。

好的,谢谢,过时的代码调用了tokenCache.GetMsalCacheInstance()传递到SecretentialClientApplicationBuilder的构造函数中,因此我可能需要在使用SecretentialClientApplicationBuilder时执行等效操作。您知道如何在使用生成器时将从GetMsalCacheInstance()返回的TokenCache获取到SecretentialClientApplication中吗?@EdJames SecretentialClientApplication.UserTokenCache.SecretentialClientApplication.UserTokenCache是只读的,我需要设置它。@EdJames我知道要传递它。为什么需要手动修改缓存?