Ryvik
Ryvik

Reputation: 473

How do I Request a Azure Graph API Access Token from Azure PowerShell for a user?

I'm familiar with requesting a token in Az CLI -- az account get-access-token --resource-type ms-graph | ConvertFrom-Json but I need this from the Azure PowerShell. I know you can request a REST API token via

$currentAzureContext = Get-AzContext
$profileClient = New-Object Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient([Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile);
$UserToken = $profileClient.AcquireAccessToken($currentAzureContext.Subscription.TenantId).AccessToken;

But again, I need a Graph access token. Any way of doing this?

Upvotes: 1

Views: 2782

Answers (1)

Alex
Alex

Reputation: 18536

Not sure if it counts as a duplicate, but this answer I got the other day for my question will solve your question as well.

Connect-AzAccount
$resource = "https://graph.microsoft.com"
$context = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile.DefaultContext
$Token = [Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.AuthenticationFactory.Authenticate($context.Account, $context.Environment, $context.Tenant.Id.ToString(), $null, [Microsoft.Azure.Commands.Common.Authentication.ShowDialog]::Never, $null, $resource).AccessToken

Upvotes: 5

Related Questions