11,567 questions
A first chance exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll
niranjan kumar
1
Reputation point
Hi, i am trying to generate the authentication token using AcquireTokenForClient but ending up with Httprequestexception, The code crashes at async method (cca.AcquireTokenForClient(ewsScopes).ExecuteAsync())
below is the code snippet (a well known from MS).
try
{
// Using Microsoft.Identity.Client 4.22.0
var cca = ConfidentialClientApplicationBuilder
.Create(sApplicationID )
.WithClientSecret(sSecretKey_id)
.WithTenantId(sTenantid )
.Build();
var ewsScopes = new string[] { "https://outlook.office365.com/.default" };
var authResult = await cca.AcquireTokenForClient(ewsScopes).ExecuteAsync();
// Configure the ExchangeService with the access token
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
ewsClient.Credentials = new OAuthCredentials(authResult.AccessToken);
ewsClient.ImpersonatedUserId =
new ImpersonatedUserId(ConnectingIdType.SmtpAddress, "******@xxtest.com");
//Include x-anchormailbox header
ewsClient.HttpHeaders.Add("X-AnchorMailbox", "******@xxtest.com");
// Make an EWS call
var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
foreach(var folder in folders)
{
Console.WriteLine("Folder: {folder.DisplayName}");
}
}
catch (MsalException ex)
{
Console.WriteLine("Error acquiring access token: {ex}");
}
catch (HttpRequestException ex)
{
//Console.WriteLine("Error: {ex}");
Console.WriteLine(ex.StackTrace);
//MessageBox.Show(ex.StackTrace );
}
Developer technologies C#
Sign in to answer