POX autodiscover requests using OAuth for O365 endpoint

nyacharya 21 Reputation points
2021-02-17T08:03:21.92+00:00

Is it possible to make a POX autodiscover request using an OAuth token. The token has application access privilege for the EWS "full access as app" Office 365 Exchange Online permission.

Whenever I make a request with the token set using Bearer authentication, I get the following response

<?xml version="1.0" encoding="utf-16"?>  
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">  
  <Response>  
    <Error Time="07:52:39.8644685" Id="1321019259">  
      <ErrorCode>500</ErrorCode>  
      <Message>The email address can't be found.</Message>  
      <DebugData />  
    </Error>  
  </Response>  
</Autodiscover>  

But the same request works with basic authentication. I am using this endpoint - https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml

The reason i am doing this is to fetch appropriate headers to make public folder requests

I am able to successfully query the SOAP autodiscover endpoint with same token.

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
526 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ben P 6 Reputation points
    2021-05-12T17:34:20.827+00:00

    I am experiencing this exact problem (here's my question), and I found that OAuth works as long as you use delegated permissions instead of application permissions. I'm not sure if this will work in your scenario or not.

    I used the EWS.AccessAsUser.All scope when requesting the token using a device code flow. Device code flow will require the following setting to be enabled in the app registration:
    96036-image.png


  2. Ben P 6 Reputation points
    2021-05-25T22:19:12.233+00:00

    @Madan Bisht here's a sample of code I used to obtain an access token that worked for me:

    IPublicClientApplication app = PublicClientApplicationBuilder  
        .Create("<your client id>")  
        .WithTenantId("<your tenant id>")  
        .Build();  
      
    AuthenticationResult authResult = null;  
      
    authResult = await app.AcquireTokenInteractive(new string[] { "EWS.AccessAsUser.All" }).ExecuteAsync();  
      
    if (authResult != null)  
    {  
        _AccessToken = authResult.AccessToken;  
    }  
    

    This will open up a login Window for authenticating with Azure AD, and if the user you are logging in as has not already consented to the requested scopes, you will be prompted to consent. The device code flow works similarly, and will ask for consent if not already granted. Another method I found useful was this one:

    authResult = await app.AcquireTokenByIntegratedWindowsAuth(new string[] { "EWS.AccessAsUser.All" }).ExecuteAsync();  
    

    This will only work if the user has already consented, but I found it useful for a background process to silently acquire a token. I was going to try caching tokens from an interactive login, but the Windows authentication worked, and was simpler.

    0 comments No comments