Unauthorized for Source Providers API

Jianjun Lv 20 Reputation points Microsoft Employee
2023-09-07T07:17:29.4566667+00:00

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;     
	} 
}
Not Monitored
Not Monitored
Tag not monitored by Microsoft.
38,579 questions
0 comments No comments
{count} votes

Accepted answer
  1. MayankBargali-MSFT 70,126 Reputation points
    2023-09-07T10:44:48.5933333+00:00

    Hi @Jianjun Lv

    Welcome to Microsoft Q&A! Thanks for posting the question.

    DevOps is currently not supported in the Q&A forums, the supported products are listed over here https://docs.microsoft.com/en-us/answers/products (more to be added later on).

    You can ask the experts in the dedicated forum over here:

    https://stackoverflow.com/questions/tagged/devops

    https://developercommunity.visualstudio.com/spaces/21/index.html

    Please 'Accept Answer' if it helped so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.