Skip to content

Instantly share code, notes, and snippets.

@aflyen
Created December 6, 2022 21:04
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/efb912dbad94a5153776d60de9a3c5ac to your computer and use it in GitHub Desktop.
Save aflyen/efb912dbad94a5153776d60de9a3c5ac to your computer and use it in GitHub Desktop.
Get access token from AAD for app with ClientId and ClientSecret in PowerShell to be used in HTTP request
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