Reputation: 1
I'm having an a 403 response when I try to get the content of a repo on GitHub.
Using a curl command " curl -L -H "Accept: application/vnd.github+json" -H "Authorization: Bearer <my_token>" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/Arcadix/Product.Fusion/contents" gives me the right content, but using C# I get a 403 error. Here is the code:
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "token123"); //token123 is replaced by my token of course
httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json");
httpClient.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
var contentsUrl = $"https://api.github.com/repos/Arcadix/Product.Fusion/contents";
var contentsJson = httpClient.GetStringAsync(contentsUrl).Result;
Console.WriteLine(contentsJson);
Upvotes: 0
Views: 311
Reputation: 31
I had the same problem. The reason that you get a 403 is because you need to add: httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
See Github API is responding with a 403 when using Request's request function
Upvotes: 3