Office365 API OutlookServicesClient获取令牌时挂起

本文关键字:挂起 令牌 获取 API OutlookServicesClient Office365 | 更新日期: 2023-09-27 17:49:27

我是从这里开始的例子:开始使用Office 365 api

当我的控制器动作执行时,它挂在var "new OutlookServicesClient"的下面一行

var authResult = await authContext.AcquireTokenSilentAsync(
    dcr.ServiceResourceId,
    new ClientCredential(this.configuration.IdaClientID, this.configuration.IdaClientSecret),
    new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));

我无法弄清楚为什么它挂起,特别是,因为AcquireTokenSilentAsync在这个调用之前的发现客户端工作得很好。

感谢您的帮助。

My Controller Action Method:

[Authorize]
public async Task<ActionResult> Index()
{    
    var contacts = new List<ContactItem>();
    var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
    var userObjectId = ClaimsPrincipal.Current.FindFirst(ClaimTypesAdditions.ObjectIdentifier).Value;
    var authContext = new AuthenticationContext(this.configuration.IdaAuthority, new AdalTokenCache(signInUserId));
    try
    {
        var discClient = new DiscoveryClient(
            new Uri(this.configuration.Office365DiscoveryServiceEndpoint),
            async () =>
            {
                var authResult = await authContext.AcquireTokenSilentAsync(
                    this.configuration.Office365DiscoveryResourceID,
                    new ClientCredential(this.configuration.IdaClientID, this.configuration.IdaClientSecret),
                    new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
                return authResult.AccessToken;
            });
        var dcr = await discClient.DiscoverCapabilityAsync("Contacts");
        var exClient = new OutlookServicesClient(
            dcr.ServiceEndpointUri,
            async () =>
            {
                var authResult = await authContext.AcquireTokenSilentAsync(
                    dcr.ServiceResourceId,
                    new ClientCredential(this.configuration.IdaClientID, this.configuration.IdaClientSecret),
                    new UserIdentifier(userObjectId, UserIdentifierType.UniqueId));
                return authResult.AccessToken;
            });
        var contactsResult = await exClient.Me.Contacts.ExecuteAsync();
        do
        {
            var c = contactsResult.CurrentPage;
            contacts.AddRange(c.Select(contact => new ContactItem { FirstName = contact.GivenName }));
            contactsResult = await contactsResult.GetNextPageAsync();
        }
        while (contactsResult != null);
    }
    catch (AdalException exception)
    {
        if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently)
        {
            authContext.TokenCache.Clear();
        }
    }
    return this.View("Index", contacts);
}

Office365 API OutlookServicesClient获取令牌时挂起

1.0.34 Microsoft.Office365.OutlookServices版本存在错误。可移植的,在OutlookServiceClient中导致死锁。恢复到1.0.22似乎可以工作。

这里有文档https://github.com/OfficeDev/O365-ASPNETMVC-Start/commit/b5652864756636a0b141c222e964aba953357e7a#diff-04c6e90faac2675aa89e2176d2eec7d8R139

和看起来在下一个版本中被修复,如图所示https://github.com/Microsoft/Vipr/commit/aaeff5cb94204c23d7501be8fa74b0260ddc52d9