Monday, April 27, 2026

Getting access token from Microsoft using Powershell

 


$tenantId   = "<your-tenant-id>"

$clientId   = "<yourclient-id>"

$client_secret= "<client-secret>"

$scope      = "api://<your-client-id>/.default"

$grant_type = "client_credentials"


$tokenUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"


$body = @{

    client_id      = "$clientId"

    client_secret  = "$client_secret"

    scope          =  "$scope"

    grant_type     = "client_credentials"

}


$graphToken = Invoke-RestMethod `

    -Method POST `

    -Uri $tokenUrl `

    -ContentType "application/x-www-form-urlencoded" `

    -Body $body



$token =  $graphToken.access_token


No comments: