title: Office 365 EWS application with OAuth authentication and impersonation - Error 401
body: <p>I was to trying to build an app that connect to EWS with OAuth and application persmissions using the following example code from Microsoft (<a href="https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-authenticate-an-ews-application-by-using-oauth</a>)</p>
  <p>I registered my application in Azure AD, set the Application permissions to full_access_as_app and generated a client secret.</p>
  <p>But always when i start the app i get a 401 error code (Error: Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed.)</p>
  <p>When i use the code for delegated permissions and entered my username and password it works, but i need the feature tu run my app in background with ImpersonatedUserId.</p>
  <pre><code>using System;
  using System.Configuration;
  using Microsoft.Exchange.WebServices.Data;
  using Microsoft.Identity.Client;
  
  namespace ews_oauth_samples
  {
      class Program
      {
          static void Main(string[] args)
          {
              MainAsync(args).Wait();
  
              if (System.Diagnostics.Debugger.IsAttached)
              {
                  Console.WriteLine(&quot;Hit any key to exit...&quot;);
                  Console.ReadKey();
              }
          }
          
          static async System.Threading.Tasks.Task MainAsync(string[] args)
          {
              // Configure the MSAL client to get tokens
              var ewsScopes = new string[] { &quot;https://outlook.office.com/.default&quot; };
  
              var app = ConfidentialClientApplicationBuilder.Create(ConfigurationManager.AppSettings[&quot;appId&quot;])
                  .WithAuthority(AzureCloudInstance.AzurePublic, ConfigurationManager.AppSettings[&quot;tenantId&quot;])
                  .WithClientSecret(ConfigurationManager.AppSettings[&quot;clientSecret&quot;])
                  .Build();
  
              AuthenticationResult result = null;
  
              try
              {
                  // Make the interactive token request
                  result = await app.AcquireTokenForClient(ewsScopes)
                      .ExecuteAsync();
  
                  // Configure the ExchangeService with the access token
                  var ewsClient = new ExchangeService();
                  ewsClient.Url = new Uri(&quot;https://outlook.office365.com/EWS/Exchange.asmx&quot;);
                  ewsClient.Credentials = new OAuthCredentials(result.AccessToken);
  
                  //Impersonate the mailbox you'd like to access.
                  ewsClient.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, &quot;test@demotenant.onmicrosoft.com&quot;);
  
                  // Make an EWS call
                  var folders = ewsClient.FindFolders(WellKnownFolderName.MsgFolderRoot, new FolderView(10));
                  foreach (var folder in folders)
                  {
                      Console.WriteLine($&quot;Folder: {folder.DisplayName}&quot;);
                  }
              }
              catch (MsalException ex)
              {
                  Console.WriteLine($&quot;Error acquiring access token: {ex.ToString()}&quot;);
              }
              catch (Exception ex)
              {
                  Console.WriteLine($&quot;Error: {ex.ToString()}&quot;);
              }
          }
      }
  }
  </code></pre>