0

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);

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.