Karthik Sivagnanam
Karthik Sivagnanam

Reputation: 19

VSTS REST API Error when using Basic Authorization (username & password)

Using postman tool I generated a bearer token with Headers, Basic authorization with username and password and Body.

I am getting an error when I am trying same through web service request in VSTS.

What is the syntax of providing username password with basic authorization?

Upvotes: 2

Views: 1590

Answers (2)

Shivaraj
Shivaraj

Reputation: 400

If you have PAT(Personal Access Token), you can use that for Basic authentication with no username and PAT in password field.enter image description here

Upvotes: 1

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30372

You can try below sample to use the username and password with Basic authorization to call the 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

Please see Choosing the right authentication mechanism for more information about the Authentication.

Upvotes: 0

Related Questions