Search code examples
adalazure-ad-msalexchange-management-shell

Acquiring OAuth2 access tokens for Exchange Management Shell cmdlets


In our python application, we use the Exchange Management Shell cmdlets to perform some tasks. (In fact, the python application runs .Net application, which calls the Exchange Management Shell cmdlets.) Currently we use basic authentication with the New-PSSession cmdlet. On the other hand, we are already acquiring OAuth2 access tokens for different APIs and resources. Now we would like to use OAuth2 authentication with the Exchange Management Shell cmdlets.

We had a look at this article[1].

However we have a few questions:

  1. Is there a way to get access tokens without using ADAL and MSAL?
  2. Is there a way to get a new access token using a refresh token?
  3. It seems there is no chance to use an external(system) browser, isn't there? Thank you in advance.

[1] https://developermessaging.azurewebsites.net/2018/09/07/acquiring-oauth2-access-tokens-for-automating-exchange-management-shell-cmdlets/?unapproved=96&moderation-hash=9250fbdda0e11fd8af9bafd4c3576139#review-96


Solution

  • Oh, in fact, everything is very simple:

    1. Request for an authorization code: https://login.microsoftonline.com/common/oauth2/authorize?redirect_uri=http%3A%2F%2Flocalhost%3A55555&client_id=a0c73c16-a7e3-4564-9a95-2bdf47383716&response_type=code
    2. Request for an access token:

      • URL: https://login.microsoftonline.com/common/oauth2/token
      • body: resource=https%3A%2F%2Foutlook.office365.com&client_id=a0c73c16-a7e3-4564-9a95-2bdf47383716&redirect_uri=http%3A%2F%2Flocalhost%3A9999&code=YOUR_AUTHORIZATION_CODE&client_secret=&grant_type=authorization_code