首页 > 解决方案 > 如何从 Azure PowerShell 为用户请求 Azure Graph API 访问令牌?

问题描述

我熟悉在 Az CLI 中请求令牌——az account get-access-token --resource-type ms-graph | ConvertFrom-Json但我需要 Azure PowerShell 中的这个。我知道您可以通过以下方式请求 REST API 令牌

$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;

但同样,我需要一个 Graph 访问令牌。有什么办法吗?

标签: azurepowershell

解决方案


不确定它是否算作重复,但我前几天得到的这个答案也可以解决你的问题。

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

推荐阅读