Skip to content

Instantly share code, notes, and snippets.

@aflyen
Created March 4, 2022 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aflyen/dfefa38aed4697c00e52eceedfddce10 to your computer and use it in GitHub Desktop.
Save aflyen/dfefa38aed4697c00e52eceedfddce10 to your computer and use it in GitHub Desktop.
Get access token from Azure Active Directory with ClientId and ClienctSecret in PowerShell
function Get-CorpMicrosoftGraphAccessToken
{
Param(
[Parameter(Mandatory=$true)]
[string]$TenantId,
[Parameter(Mandatory=$true)]
[string]$ClientId,
[Parameter(Mandatory=$true)]
[string]$ClientSecret
)
$Body = @{
tenant = $TenantId
client_id = $ClientId
client_secret = $ClientSecret
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
}
$Response = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" -Body $Body -Method Post -ContentType "application/x-www-form-urlencoded"
return $Response.access_token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment