我继承了一个与VSTS API集成的PowerShell脚本,并使用当前存储在我们团队密码保护中的PAT(个人访问令牌)进行身份验证。
然而,PAT的起源已经失去了时间的迷雾,我不知道团队的哪个成员首先创建它(他们可能甚至不在这里工作!),或者哪个用户&# 39; s VSTS帐户,它定义于。
使用只是 PAT,VSTS API中是否有任何" who-am-i" -type端点可以回复用户,guid,或者PAT定义的帐户的其他细节?
有几个原因我特别想知道定义PAT的位置,而不是仅从另一个帐户发出新的:
干杯,
中号
答案 0 :(得分:1)
我已经看到这成功地用于获取PAT的所有者: https://docs.microsoft.com/en-us/javascript/api/azure-devops-extension-api/connectiondata
例如使用API调用: https://dev.azure.com/domoreexp/_apis/connectionData?api-version=5.0-preview
您可以获取authenticatedUser和AuthorizedUser(不确定tbh有什么区别)
答案 1 :(得分:0)
VSTS本身无法显示创建PAT的人。
但是如果你知道PAT的价值,你可以通过创建一个wotk项找到创建PAT的用户。例如,您可以使用PAT创建Task wotk项目,然后创建Task wotj项目的用户是创建PAT的个人。
此外,您可以通过创建user voice来反馈此功能。
答案 2 :(得分:0)
根据Marina的回答,我编写了以下PowerShell脚本,以使用PAT进行身份验证来创建新任务。然后,您可以检查json响应中的“fields - > System.CreatedBy”属性,以查看PAT所属的帐户。
$ErrorActionPreference = "Stop";
$ProgressPreference = "SilentlyContinue";
Set-StrictMode -Version "Latest";
$vstsAccount = "myaccount"; # e.g. "myaccount" in "https://myaccount.visualstudio.com"
$vstsProjectName = "myprojectname"; # e.g. "myprojectname" in "https://myaccount.visualstudio.com/myprojectname"
$vstsPat = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
function Invoke-VstsWebRequest
{
param
(
[string] $Uri,
[string] $Pat,
[string] $Method,
[string] $Body,
[string] $ContentType
)
write-host "uri = '$Uri'";
$splat = @{
"Uri" = $Uri
"Headers" = @{
"Authorization" = "Basic " +
[System.Convert]::ToBase64String(
[System.Text.ASCIIEncoding]::ASCII.GetBytes(
[string]::Format("{0}:{1}", "", $Pat)
)
)
}
"Method" = $Method
"UseBasicParsing" = $true
}
if( -not [string]::IsNullOrEmpty($Body) )
{
write-host "body = ";
write-host $Body;
$splat.Add("ContentType", $ContentType);
$splat.Add("Body", $Body);
}
$response = Invoke-WebRequest @splat;
write-host "response = ";
write-host ($response.Content | ConvertFrom-Json | ConvertTo-Json);
return $response.Content;
}
# get existing work items in the specified project
# (not necessary, but useful for testing)
$uri = "https://$vstsAccount.visualstudio.com/$vstsProjectName/_apis/wit/wiql?%24top=50&api-version=4.1";
$query = @"
SELECT [System.ID],
[System.Title]
FROM workitems
WHERE [System.TeamProject] = '{0}'
ORDER BY [System.Title]
"@;
$body = ConvertTo-Json ([ordered] @{
"query" = [string]::Format($query, $vstsProjectName)
});
$type = "application/json";
$json = Invoke-VstsWebRequest -Uri $uri -Pat $vstsPat -Method "Post" -Body $body -ContentType $type;
# create a new "Task" work item in the specified project
# (the response will show the user account associated with the PAT)
$uri = "https://$vstsAccount.visualstudio.com/$vstsProjectName/_apis/wit/workitems/`$Task?api-version=4.1";
$body = ConvertTo-Json @([ordered] @{
"op" = "add"
"path" = "/fields/System.Title"
"value" = "my sample task"
});
$type = "application/json-patch+json";
$json = Invoke-VstsWebRequest -Uri $uri -Pat $vstsPat -Method "Post" -Body $body -ContentType $type;
输出:
...
"System.CreatedBy":"My Username <my.username@example.org>"
...
P.S。我在这里发布了一个UserVoice票证,请求“who-am-i”端点:https://visualstudio.uservoice.com/forums/330519-visual-studio-team-services/suggestions/34509568-provide-a-who-am-i-endpoint-in-the-vsts-api-to-i