Azure ClientSecretCredential身份验证失败:配置问题正在阻止身份验证-有关详细信息,请查看来自服务器的错误消息

Azure ClientSecretCredential身份验证失败:配置问题正在阻止身份验证-有关详细信息,请查看来自服务器的错误消息,azure,azure-devops,azure-active-directory,Azure,Azure Devops,Azure Active Directory,我正在尝试使用以下代码获取服务主体的访问令牌 $authUrl = "https://login.windows.net/" + $tenantid + "/oauth2/token/" $body = @{ grant_type = "client_credentials" client_id = $serviceprincipalid resource = "https://manage

我正在尝试使用以下代码获取服务主体的访问令牌

$authUrl = "https://login.windows.net/" + $tenantid + "/oauth2/token/"
$body = @{
  grant_type    = "client_credentials"
  client_id     = $serviceprincipalid
  resource      = "https://management.azure.com/"
  client_secret = $serviceprincipalkey
};
$response = Invoke-RestMethod –Uri $authUrl –Method POST –Body $body

Write-Host $response
Write-Output $response.access_token
##vso[task.setvariable variable=myToken;]$response.access_token
上面的代码在我的本地机器的PowerShell上运行得非常好,但是当我在Azure DevOps管道中运行相同的代码库时,它给出了以下错误

ClientSecretCredential authentication failed: A configuration issue is preventing 
authentication - check the error message from the server for details. You can modify the 
configuration in the application registration portal. See https://aka.ms/msal-net-invalid- 
client for details.  Original exception: AADSTS7000222: The provided client secret keys are 
expired. Visit the Azure Portal to create new keys for your app, or consider using certificate 
credentials for added security: https://docs.microsoft.com/azure/active- 
directory/develop/active-directory-certificate-credentials
Trace ID: 98787ui7-e8ae-4712-b8b5-7678u8765rt5
Correlation ID: yhjnbv43-56sy-9ksy-b8b5-mj876yu78i90
Timestamp: 2021-03-16 12:32:28Z
There was an error with the service principal used for the deployment.`
我检查了密钥,但是密钥没有过期,它的过期日期已经设置为2022年。如果它将过期,那么代码不应该在我本地机器的PowerShell中工作


有人知道吗?请让我知道如何解决此问题。

事实上,错误不是由上面的脚本引起的,根据我的测试,它在devops中工作正常

如果您使用,它将允许您配置要使用的,当您运行任务时,它将自动将Azure powershell与服务连接中配置的服务主体连接。该错误是由服务连接中配置的服务主体的过期机密引起的,而不是脚本中的机密

我也可以把你的问题转述给我

要解决此问题,请执行以下步骤

1.导航到Azure PowerShell任务,检查您使用的服务连接

2.导航到devops中的
项目设置
服务连接
->找到您使用的连接->单击它->
管理服务主体

然后它将打开相关的广告应用程序页面,只需创建一个新的机密和服务连接,在Azure Powershell任务中使用它,按照我提到的相同步骤操作

3.配置完成后,再次测试,工作正常


您好,Joy,我已投票支持您的答案,我检查后发现由于应用程序机密过期,服务连接失败,因为添加了新的机密,并且我的解决方案开始工作,我将接受您的建议作为答案。:)