许多 Azure DevOps API 在引用用户时都将存储密钥作为输入。该存储密钥是您所在组织中用户的唯一标识符。我似乎无法弄清楚如何确定当前登录用户的存储密钥。
我知道我可以调用 Get Profile API 并指定
Me
作为 id,但这通常会返回有关当前用户的信息 - 与特定组织无关。Me
来获取当前用户。通过
Get Profile API得到的
descriptor
,它只能代表用户自己。换句话说,它是访问服务器的身份dev.azure.com
(不是组织,只是由服务器识别)。所以这里的descriptor
不能用于Org,因为无法在其中识别。
此外,对于Get User api和List User api,它们都是组织级别的,这意味着它的查询结果将是整个组织的查询结果。而且我们没有为 Azure Devops rest api 提供
filter
功能。
所以,如果想
filter
通过在 API url 中指定一个用户,不幸的是,抱歉,当您使用 Azure Devops rest api 时,这不支持。您必须遵循文档中显示的限制 URL 格式。
作为解决方法,您可以使用 powershell 脚本来实现过滤器。
您可以在 Azure devops 管道中执行脚本:
$url = 'https://vssps.dev.azure.com/{org}/_apis/graph/users?api-version=5.0-preview.1';
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"} -Method Get
$results = $response.value | Where {$_.displayname -eq "{your account name}"} #|
Write-Host "results = $($results | ConvertTo-Json -Depth 100)"
Write-Host $results.desciptor
或者在Powershell ISE中本地运行,比上一个更方便:
然后您可以将
desciptor
存储到一个参数中并在Storage Keys - Get API中使用它。
connectionData
API 端点可能对您有所帮助。似乎微软忘记了正确记录它,但返回的对象是通过 azure-devops-extension-api JavaScript 库记录在https://learn.microsoft.com/en-us/javascript/api/azure- devops-extension-api/connectiondata.
这里是一个使用 Azure DevOps 管道的例子
$contentType = "application/json";
$headers = @{ Authorization = 'Bearer $(System.AccessToken)' };
$uri = "$(System.CollectionUri)/_apis/connectionData?api-version=7.0-preview.1";
$connectionData = Invoke-RestMethod -uri $uri -method GET -Headers $headers -ContentType $contentType
# The VSID a.k.a. Storage Key of the current user, E.g. "e2f6c43a-c231-4f82-841a-bfc82920ffaa"
$authorizedUserVSId = $connectionData.authorizedUser.id
# The "old" Identity Descriptor, e.g. "Microsoft.IdentityModel.Claims.ClaimsIdentity;9e57a836-f042-42ae-83bd-49f609cbd058\[email protected]"
$authorizedUserIdentityDescriptor = $connectionData.authorizedUser.descriptor
# The "new" Graph User Descriptor a.k.a Subject Descriptor. E.g. "aad.ZTJmNmM0M2EtYzIzMS00ZjgyLTg0MWEtYmZjODI5MjBmZmFh"
$authorizedUserSubjectDescriptor = $connectionData.authorizedUser.subjectDescriptor