PowerShell Invoke-RestMethod未使用PAT进行Azure DevOps身份验证

3

我正在尝试调用我们Azure DevOps项目的REST API,但得到了一些我不希望看到的结果。

使用LinqPad可以获取结果,但使用Powershell会失败。

我的脚本

$env:Build_BuildId = 2468
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("myPAT"))
$env:System_TeamProject = "myProject"

$url = "https://dev.azure.com/myorg/$env:System_TeamProject/_apis/build/builds/$env:Build_BuildId/changes?api-version=5.0"

$response = Invoke-RestMethod -Uri $url -Method Get -ContentType "application/json" -Headers @{ 
    Authorization = "Basic $token" 
}

Write-Host $response

响应

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">




<html lang="en-US">
<head><title>

            Azure DevOps Services | Sign In

</title><meta http-equiv="X-UA-Compatible" content="IE=11;&#32;IE=10;&#32;IE=9;&#32;IE=8" />
    <link rel="SHORTCUT ICON" href="/favicon.ico"/>

    <link data-bundlelength="508485" data-bundlename="commoncss" data-highcontrast="/_static/tfs/M154_20190628.18/_cssbundles/HighContrast/vss-bundle-commoncss-vAEI_yKFIiS9jTVmCtAOiwe4cLPqdXnp6QCUVseU7jzk=" data-includedstyles="jQueryUI-Modified;Core;Splitter;PivotView" href="/_static/tfs/M154_20190628.18/_cssbundles/Default/vss-bundle-commoncss-vqjKBNZxfVQkGGn0rrvF7eh9DJDj__wqtFN85fVrIQn8=" rel="stylesheet" />
<link data-bundlelength="116162" data-bundlename="viewcss" data-highcontrast="/_static/tfs/M154_20190628.18/_cssbundles/HighContrast/vss-bundle-viewcss-v356iHjTFccxhkNidRJIEefQ92VqpWpa7rO4mdtAnDpM=" data-includedstyles="VSS.Controls" href="/_static/tfs/M154_20190628.18/_cssbundles/Default/vss-bundle-viewcss-vXHgBtK2hntEJYzWnMNhcJkJC-nUhp2m3BtF-jVlzOZA=" rel="stylesheet" />
... etc. etc.

我想象中,请求会得到一个响应,该响应将被渲染为Azure DevOps的登录页面。

enter image description here

我已经仔细检查了我的PAT,甚至创建了几个新的完全访问版本,但行为没有改变。


最终这个脚本将在托管代理上运行,所以我不太担心最终的结果,但我希望如果不需要就不要运行测试脚本的流水线。


1
看起来你没有正确编码PAT。根据文档的说明,应该像这样:[Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$PersonalAccessToken)))$user参数通常为空。 - boxdog
3个回答

7

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":myPAT")) 是正确的写法,myPAT 前应加冒号。

例如:

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":myPAT"))

2

对于使用 PowerShell 和 Azure DevOps 并获取 Azure DevOps 登录 HTML 响应/返回的任何人,以下是解决方案 -

(注意:您可以将 projectId 替换为项目名称,将 repoId 替换为仓库名称)

$personalToken = "your pat token"
$patToken = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
$repoHeader = @{"Authorization"="Basic $patToken"}
Write-Output $repoHeader
$projectId = "your project id here"
$repoId = "your repo id here"
$orgName = "your org name here"
$repoUrl = [string]::Format("https://dev.azure.com/{0}/{1}/_apis/git/repositories/{2}/refs?api-version=6.0", $orgName, $projectId, $repoId)
Write-Output $repoUrl
$output = Invoke-RestMethod -Uri $repoUrl -Method Get -ContentType "application/json; charset=utf-8; api-version=6.0" -Headers $repoHeader -MaximumRedirection 10
Write-Output $output
foreach ($outputValue in $output.value)
{
    Write-Output $outputValue.name
    Write-Output "Hi"
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接