I'm trying to migrate from TFS to Git. In my Visual Studio asp.net solution I use the api to retrieve certain information. First I try in my soltuion to get the list of repositories with the 'api-url' https://dev.azure.com/{..}/{..}/_apis/git/repositories.
client = new HttpClient();
string url = "https://dev.azure.com/pragmalogicdev/apps/_apis/git/repositories";
var response = client.GetAsync(url).Result;
string resultStr = client.GetStringAsync(url).Result;
But response.statuscode is always 203 and resultStr does not contain the list of repositories. if I type the 'api-url' directly into the url bar of the browser, I get the correct result: 2 repositories.
What should I do to get the correct result in the source of my solution?
I will be glad if someone can help me!
Thanks in advance!
Cor
CodePudding user response:
response.statuscode is always 203 and resultStr does not contain the list of repositories.
The 203 means authentication issues when running the code.
From your code sample, you didn't add the authentication info to the http request.
To solve the issue, you can create PAT (Personal Access Token) in Azure DevOps and then use the PAT for authentication.
Here is the code sample:
static void Main(string[] args)
{
string personalToken = "PAT";
var base64Token = Convert.ToBase64String(Encoding.ASCII.GetBytes($":{personalToken}"));
string url = "https://dev.azure.com/ORG/PROJECT/_apis/git/repositories?api-version=6.0";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64Token);
string resultStr = client.GetStringAsync(url).Result;
var response = client.GetAsync(url).Result;
Console.WriteLine(resultStr);
Console.WriteLine(response);
}
}