When working with REST APIs one might need a Bearer Token quite often. Here is one way to retrieve it through PowerShell for a Service Principal.
function Get-AzOauth2Token
{
[CmdletBinding()]
Param
(
[string]$TenantId,
[string]$AppId,
[string]$Secret
)
$result = Invoke-RestMethod -Uri $('https://login.microsoftonline.com/'+$TenantId+'/oauth2/token?api-version=1.0') -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$AppId"; "client_secret" = "$Secret" }
$authorization = ("{0} {1}" -f $result.token_type, $result.access_token)
return $authorization
}
Example:
$token = Get-AzOauth2Token -TenantId tenant_id -AppId app_id -Secret secret_value
Top comments (0)