简体   繁体   English

Azure DevOps 构建代理在其令牌缓存中没有访问令牌

[英]Azure DevOps build agent does not have accesstokens in its tokencache

I am trying to invoke rest api from within Azure DevOps build agent.我正在尝试从 Azure DevOps 构建代理中调用 rest api。 For that I need bearertoken which I can get in my local machine with:为此,我需要 Bearertoken,我可以通过以下方式在我的本地机器中获取它:

$accessToken = ((Get-AzContext).TokenCache.ReadItems() | Where { $_.TenantId -eq (Get-AzContext).Tenant } | Sort-Object -Property ExpiresOn -Descending)[0].AccessToken

However, on the build agent this never returns any tokens.但是,在构建代理上,这永远不会返回任何令牌。 Is there some other way I should try to get that token?还有其他方法我应该尝试获取该令牌吗?

Have you enabled the 'Allow scripts to access the OAuth token' setting in the Agents settings?您是否在代理设置中启用了“允许脚本访问 OAuth 令牌”设置?

在此处输入图像描述

This would be for builds and releases using the visual designer.这适用于使用视觉设计器的构建和发布。

For YAML builds see the predefined variable documentation .对于 YAML 构建,请参阅预定义变量文档 There are notes about using the "System.AccessToken" variable in a script.有关于在脚本中使用“System.AccessToken”变量的说明。 Essentially, you must explicitly map System.AccessToken into the pipeline using a variable.本质上,您必须使用变量将 map System.AccessToken 显式放入管道中。 You can do this at the step or task level:您可以在步骤或任务级别执行此操作:

steps:
  - bash: echo This is a script that could use $SYSTEM_ACCESSTOKEN
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
  - powershell: Write-Host "This is a script that could use $env:SYSTEM_ACCESSTOKEN"
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)

Thanks for the suggestions and ideas.感谢您的建议和想法。 Answer was too simple to figure it out.答案太简单了,无法弄清楚。 I ripped most of the oneliner and now it works by only using:我撕掉了大部分的oneliner,现在它只使用:

$accessToken = ((Get-AzContext).TokenCache.ReadItems()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM