我想得到一个 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;
}
我傻了,忘了 await 这个方法。
GraphServiceClient graphClient = await GetGraphApiClient();