I used same auth(basic auth based on personal pat token) for two APIs "Code Search Results" and "Source Providers".
But the first one succeed and the second one failed with 401(Unauthorized) error code.
Doc link and sample code:
Code Search Results - Fetch Code Search Results
var requestUrl = $"https://almsearch.dev.azure.com/{this.Organization}/{this.Project}/_apis/search/codesearchresults?api-version=6.1-preview.1";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", string.Empty, this.personalAccessToken))));
string json = CreateRequestBodyJson(searchText);
using (HttpResponseMessage response = await client.PostAsync(requestUrl,
new StringContent(json, Encoding.UTF8, "application/json")))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
}
Source Providers - Get File Contents
var requestUrl = $"https://dev.azure.com/{this.Organization}/{this.Project}/_apis/sourceproviders/TfsGit/filecontents?repository={repository}&commitOrBranch=master&api-version=6.1-preview.1&path={path}";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Basic",
Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", string.Empty, this.personalAccessToken))));
using (HttpResponseMessage response = await client.GetAsync(requestUrl))
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
return responseBody;
}
}