Created
March 4, 2022 14:41
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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