Get-MsalToken 错误 AADSTS7000218:请求正文必须包含以下参数:“client_assertion”或“client_secret”

blo*_*s4t 12 powershell azure-active-directory azure-ad-msal

这是我的 PowerShell 脚本

$connectionDetails = @{
    'TenantId'    = '****-****'
    'ClientId'    = '****-****'
    'Interactive' = $true
    'Scopes' = '****-****'
    'RedirectUri' = '****-****'
}

$token = Get-MsalToken @connectionDetails

$accessToken = $token.AccessToken

write-output $accessToken
Run Code Online (Sandbox Code Playgroud)

其错误如下:

AADSTS7000218:请求正文必须包含以下参数:“client_assertion”或“client_secret”

为什么我会收到此错误?我该如何修复它?

All*_* Wu 16

请参阅公共客户端和机密客户端应用程序之间的差异

机密客户端应用程序可以安全地保守应用程序的秘密,而公共客户端则不然。

因此,您遇到的错误意味着您的应用程序注册是 Confidential client application (当您创建它时,您选择Web),它要求您提供ClientSecret,但您没有指定它。

在此输入图像描述

所以你有两个选择来解决它。

第一个是ClientSecret在您的脚本中提供(不要修改 Azure 门户上的任何内容):

$connectionDetails = @{
    'TenantId'    = '****-****'
    'ClientId'    = '****-****'
    'ClientSecret'= '****-****'
    'Interactive' = $true
    'Scopes' = '****-****'
    'RedirectUri' = '****-****'
}
Run Code Online (Sandbox Code Playgroud)

第二个是将应用程序注册从机密客户端应用程序更改为 Azure 门户上的公共客户端应用程序 -> 应用程序注册 -> 清单。(不要修改你的脚本

在此输入图像描述

Get -MsalToken.ps1还包括 2 个示例:

.EXAMPLE
    PS C:\>Get-MsalToken -ClientId '00000000-0000-0000-0000-000000000000' -TenantId '00000000-0000-0000-0000-000000000000' -Interactive -Scope 'https://graph.microsoft.com/User.Read' -LoginHint user@domain.com
    Force interactive authentication to get AccessToken (with MS Graph permissions User.Read) and IdToken for specific Azure AD tenant and UPN using client id from application registration (public client).

.EXAMPLE
    PS C:\>Get-MsalToken -ClientId '00000000-0000-0000-0000-000000000000' -ClientSecret (ConvertTo-SecureString 'SuperSecretString' -AsPlainText -Force) -TenantId '00000000-0000-0000-0000-000000000000' -Scope 'https://graph.microsoft.com/.default'
    Get AccessToken (with MS Graph permissions .Default) and IdToken for specific Azure AD tenant using client id and secret from application registration (confidential client).
Run Code Online (Sandbox Code Playgroud)