When I try to read the mailbox in the following way, I can login the user and get tokens, but when I try to reach the mailbox, the remote server returned an error “: (401) Not approved.” I take his mistake
The Remote server returned an error when we tried to access the office 365 user mailbox with the following code: (401) Not confirmed.” we take his mistake
var username = "EMAIL";
var password = new NetworkCredential("", "PASSWORD").SecurePassword;
var client = PublicClientApplicationBuilder
.Create("ClientID")
.WithTenantId("TenantId”)
.Build();
var scopes = new string[] { "user.read", "mail.read" };
var result = await client.AcquireTokenByUsernamePassword(scopes, username, password).ExecuteAsync();
var ewsClient = new ExchangeService();
ewsClient.Url = new Uri(https://outlook.office365.com/EWS/Exchange.asmx);
ewsClient.Credentials = new OAuthCredentials(result.AccessToken);
ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, username);
ewsClient.HttpHeaders.Add("X-AnchorMailbox", username);
Mailbox mb = new Mailbox(username);
FolderId fid = new FolderId(WellKnownFolderName.Inbox, mb);
Folder inbox = Folder.Bind(ewsClient, fid);