为什么即使我有足够的权限也无法访问 VSTS 分析 API 服务

Why can't I access the VSTS analytics API service even though I have sufficient permissions

我尝试从 powershell 脚本调用私人代理的分析 API,但收到以下错误响应: [error]Invoke-RestMethod : {"error":{"code":"0","message":"Using this service requires \"View Analytics\" permission on

我检查了 "View analytics" 的安全设置,它显示 'Allow'。

我为私人代理设置的 PAT 启用了所有范围。

我可以使用另一个具有 Basic 授权的 PAT 通过 Postman 访问此 API。

此外,如果我将我的 powershell 脚本中的 header 更改为:

$header =  @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}

至:

$header =  @{Authorization = "Basic <hard coded base64 encoded string used in the header for my Postman queries>"}

有效。

问题:这是怎么回事?

System_Accesstoken 是在 build/release 期间动态生成的令牌,它不是您用来配置代理的令牌。

您用于配置代理的令牌仅用于代理的初始配置。

向 build/release 的服务帐户授予权限:

  1. 转到团队项目的安全页面
  2. 在过滤用户和组搜索框中输入 project collection build service 和 select Project Collection build Service (xxx) 用户(不是组)
  3. 设置View analytics允许。

您需要允许脚本访问 OAuth 令牌。为此,请转到您的管道,select 阶段并开启 运行 代理启用 "Allow scripts to access the OAuth token"。

How to Allow scripts to access the OAuth token

除此之外,您还必须授予用户 "Project Collection build Service (xxx)" 的访问权限以实现您想要的请求,在本例中为 "View analytics",但它可以是编辑任务组、编辑管道等。