需要向 azure devops api 请求添加身份验证 header
need to add authentication header to azure devops api request
我正在尝试通过向 Azure DevOps REST 发送 GET 请求来获取有关我最新版本的信息 Api。我正在使用带有补丁 1 更新的 Azure DevOps Server 2020。我需要在请求中添加授权 header。我添加的header没用
我正在 Powershell 中执行请求。这是我的代码:
$PAT = 'personal access token'
$ENCODED = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($PAT))
$headers = @{
Authorization="Basic $ENCODED"
}
Invoke-RestMethod -Uri [azure devops server url]/[project name]/_apis/build/latest/Build?api-version=5.0 -Method Get -Headers $headers
当我 运行 代码时出现错误:Invoke Method: The format of value [PAT] is invalid
更新:
我更新了 header 语法。现在我得到的回应是:
Invoke-RestMethod:
TF400813: Resource not available for anonymous access. Client authentication required. - Azure DevOps Server
我还尝试在 header 中传递我的 Azure DevOps 用户名和密码,如下所示:
$headers = @{
Authorization="Basic [domain\username]:[password]"
}
我得到了这样的回应:
Invoke-RestMethod: Response status code does not indicate success: 401 (Unauthorized).
我是否必须在 Azure DevOps 中启用某些设置?
我经常在 PowerShell 中参考此演示 运行 REST API,它可以正常工作:
$uri = "request URI"
$pat = "personal access token"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json")
. . .
$body = "{
. . .
}"
Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -Method POST
在你的情况下,问题似乎是由编码引起的。尝试使用 ASCII
或 UTF8
,而不是 Unicode
.
查看更多详情,您可以查看“Use personal access tokens”。
我正在尝试通过向 Azure DevOps REST 发送 GET 请求来获取有关我最新版本的信息 Api。我正在使用带有补丁 1 更新的 Azure DevOps Server 2020。我需要在请求中添加授权 header。我添加的header没用
我正在 Powershell 中执行请求。这是我的代码:
$PAT = 'personal access token'
$ENCODED = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($PAT))
$headers = @{
Authorization="Basic $ENCODED"
}
Invoke-RestMethod -Uri [azure devops server url]/[project name]/_apis/build/latest/Build?api-version=5.0 -Method Get -Headers $headers
当我 运行 代码时出现错误:Invoke Method: The format of value [PAT] is invalid
更新: 我更新了 header 语法。现在我得到的回应是:
Invoke-RestMethod:
TF400813: Resource not available for anonymous access. Client authentication required. - Azure DevOps Server
我还尝试在 header 中传递我的 Azure DevOps 用户名和密码,如下所示:
$headers = @{
Authorization="Basic [domain\username]:[password]"
}
我得到了这样的回应:
Invoke-RestMethod: Response status code does not indicate success: 401 (Unauthorized).
我是否必须在 Azure DevOps 中启用某些设置?
我经常在 PowerShell 中参考此演示 运行 REST API,它可以正常工作:
$uri = "request URI"
$pat = "personal access token"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", ("Basic {0}" -f $base64AuthInfo))
$headers.Add("Content-Type", "application/json")
. . .
$body = "{
. . .
}"
Invoke-RestMethod -Uri $uri -Headers $headers -Body $body -Method POST
在你的情况下,问题似乎是由编码引起的。尝试使用 ASCII
或 UTF8
,而不是 Unicode
.
查看更多详情,您可以查看“Use personal access tokens”。