GetAccessTokenAsync静默失败,无法在调试器中发现问题。

问题描述 投票:0回答:1

我想得到一个 GraphServiceClient 这样我就可以与Teams集成。

当我试图获得一个令牌以便我可以用AAD自动识别时,这行字是

accessToken = await azureServiceTokenProvide.GetAccessTokenAsync("https://graph.microsoft.com/");

无声地失败,应用程序退出,我无法捕获任何异常或看到问题。

private async Task<GraphServiceClient> GetGraphApiClient()
{
    var azureServiceTokenProvider = new AzureServiceTokenProvider();
    string accessToken = "";
    try 
    {
        accessToken = await azureServiceTokenProvider
            .GetAccessTokenAsync("https://graph.microsoft.com/");
    } catch(AggregateException e)
    {
        Console.WriteLine("");
    } catch (Exception ex)
    {
        Console.WriteLine("");
    }


    var graphServiceClient = new GraphServiceClient(
        new DelegateAuthenticationProvider((requestMessage) =>
        {
            requestMessage
        .Headers
        .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

            return Task.CompletedTask;
        }));

    return graphServiceClient;
}
c# azure-active-directory microsoft-graph microsoft-teams microsoft-graph-teams
1个回答
0
投票

我傻了,忘了 await 这个方法。

GraphServiceClient graphClient = await GetGraphApiClient();

© www.soinside.com 2019 - 2024. All rights reserved.