Microsoft’s OAuth2 implementation: Using Microsoft Graph API

3 Minute Read


In this third and final part of the “Understanding Microsoft’s OAuth2 Implementation” series, we will be using the application that we have previously created to authenticate to the Microsoft Graph API.

If you have not done so, please read Part 1 and Part 2 before continuing.

Now, let’s start using the Microsoft Graph API using PowerShell Core!

Using PowerShell Core, we need to generate our “Authorization” URL. We can do this using [Get-MSGraphOAuthCode.ps1].

$ClientId = 'a6b6008e-1cd0-42b7-9649-3d4fec3e3748'
$redirectURL = 'https://localhost'
$scopes = ""
Get-MSGraphOAuthCode -ClientId $ClientId -RedirectUrl $redirectURL -Scope $scopes

As you can see, we are calling Get-MSGraphOAuthCode and providing our Client ID (Application ID), redirect URL, and our Scopes to the function. In return we will get a encoded URL that we will need to click and authorize our application based on our provided scopes. You should receive output similar to what follows below.

Copy this URL into your browser and authenticate using the appropriate credentials:"

Once you copy this URL into your browser, you should be redirected to a login page where you will sign in as the user you are wanting to use this application. Once you have logged in, you will be presented with a dialog that you must accept permissions based on the provided scopes:

Once you click “Accept,” your browser will be redirected to your provided redirect URL. Note, the redirect URL you provide to both your application registration and PowerShell function must match explicitly.

When redirected, you should see a URL similar to this in your browser:


Congratulations! You now have an Authorization Code to interact with the Microsoft Graph API. As you can see, your code is contained within the “code” URL parameters:


This Authorization Code is only good for 10 minutes, so we will need to either use it to request an Access Token (below), or you will need to reissue this command to get a new Authorization Code.

To get an access and refresh token, you must to provide the following information:

  • ClientID (Application ID generated when creating a new application)
  • ClientSecret (Generated Password from application registration)
  • AuthCode (Authorization Code returned from the Get-MSOAuthCode)
  • Scope (Scopes defined during application registration)
  • RedirectURL (Redirect URL defined during application registration)

You can use [Request-MSGraphAccessToken.ps1] PowerShell Core function to get a access_token and refresh_token using your Authorization Code from before. Call this function by providing these parameters:

$ClientId = 'a6b6008e-1cd0-42b7-9649-3d4fec3e3748'
$ClientSecret = 'ewwgyTWNYK80)@@?rlCC874'
$redirectURL = 'https://localhost'
$scopes = ""
$authCode =
Request-MSGraphAccessToken -ClientId $ClientId -ClientSecret $ClientSecret -AuthCode
$authCode -RedirectUrl $redirectURL -Scope $scopes

Once you call this function, you will receive a similar output to this screenshot, which contains both an access_token and a refresh_token:

You will need to keep track of both tokens in some manner. Typical implementations around OAuth 2.0 store these tokens along with timestamps in either the Windows Registry or in a configuration file (json). Either way, you need the access_token when making calls to the Microsoft Graph API.

To determine how long your access_token is valid, you will need to convert the expires_in value from seconds to your desired timestamp. An example would be to use the Get-Date CmdLet and adding 3600 seconds to the current time:


Using the two functions above, you can call the following to get access to the Microsoft Graph API People endpoint:

$ClientId = 'a6b6008e-1cd0-42b7-9649-3d4fec3e3748'
$ClientSecret = 'ewwgyTWNYK80)@@?rlCC874'
$redirectURL = 'https://localhost'
$scopes = ""
$Authorization = Get-MSGraphOAuthCode -ClientId $ClientId -RedirectUrl $redirectURL
-Scope $scopes
# Get our authorization code from our web browser and set it as the value for our
$AuthCode variable
$authCode =
$Tokens = Request-AccessToken -ClientId $ClientId -ClientSecret $ClientSecret
-AuthCode $authCode -RedirectUrl $redirectURL -Scope $scopes
# Now we can call the people endpoint on the Graph API
$params = @{
 Method = 'Get'
 Method = 'Get'
 Authorization = "bearer $($Tokens.access_token)"
 Uri = ''
# Call the Graph API and loop through each of the returned values and output them
((Invoke-RestMethod @params).value).ForEach({
 Write-Output $_

That’s it! You have successfully created an application and interacted with the Microsoft Graph API using the Auth Code Grant flow. You registered your application, requested an authorization code, received access_token and refresh_token and queries the Graph API.

The Microsoft OAuth 2.0 implementation can seem daunting at first, but once you understand the nuances and jargon, it is actually fairly straight-forward. I hope this article has helped you get a better understanding of OAuth2, especially Microsoft’s implementation when interacting with Azure resources.

Continue the conversation

SecOps Hub is a community of security pros gathered to discuss SecOps strategies, incident response best practices and ways to simplify SecOps with security orchestration, automation and response (SOAR). Continue this conversation and jump in on conversations like this one today!

Join Now

Request a Live Demo