Home > Mobile >  C# - 404 whith GitHub API request
C# - 404 whith GitHub API request

Time:12-30

I'm having an a 404 response when I try to get the latest release of a repo on GitHub. Using a browser and accessing for example the url "https://api.github.com/repos/python/cpython/releases/latest/" gives me the right JSON, but using C# I get a 404 error. The code worked perfectly yesterday but after trying it again today without changing anything, I got errors. Here is the code:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("User-Agent", "request");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", "token token123"); //token123 is replaced by my token of course
var contentsUrl = $"https://api.github.com/repos/python/cpython/releases/latest/";
var contentsJson = httpClient.GetStringAsync(contentsUrl);
Console.WriteLine(contentsJson.Result);

CodePudding user response:

The main problem is here:

new AuthenticationHeaderValue("Authorization", "token token123");

As you can see in docs the first argument is used for Scheme, Bearer for example.

 new AuthenticationHeaderValue("Bearer", ACCESS_TOKEN);

If your Scheme is token, then you should use something like this:

 new AuthenticationHeaderValue("token", "token123");
  • Related