使用邮递员工具,我生成了带有标头,带有用户名和密码以及正文的基本授权的承载令牌。
通过VSTS中的Web服务请求尝试进行操作时出现错误。
提供具有基本授权的用户名密码的语法是什么?
答案 0 :(得分:0)
您可以尝试在下面的示例中使用具有基本授权的用户名和密码来调用REST API:
C#:
var httpClient = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "Domain\\username", "password"));
httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
var response = await httpClient.GetStringAsync("https://account.visualstudio.com/_apis/git/repositories");
Console.WriteLine(response);
PowerShell:
Param(
[string]$collectionurl = "https://account.visualstudio.com",
[string]$project = "ProjectName",
[string]$user = "username",
[string]$token = "Password/PAT"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$baseUrl = "$collectionurl/$project/_apis/wit/reporting/workitemrevisions?includeLatestOnly=true&api-version=5.0-preview.2"
$response = (Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}).values
有关身份验证的更多信息,请参见Choosing the right authentication mechanism。
答案 1 :(得分:0)