-
-
Save MaximRouiller/74ae40aa994579393f52747e78f26441 to your computer and use it in GitHub Desktop.
| public class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| Task.WaitAll(ExecuteAsync()); | |
| Console.ReadLine(); | |
| } | |
| public static async Task ExecuteAsync() | |
| { | |
| HttpClient client = new HttpClient(); | |
| client.BaseAddress = new Uri("https://api.github.com"); | |
| var token = "<token>"; | |
| client.DefaultRequestHeaders.UserAgent.Add(new System.Net.Http.Headers.ProductInfoHeaderValue("AppName", "1.0")); | |
| client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); | |
| client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Token", token); | |
| var response = await client.GetAsync("/user"); | |
| } | |
| } |
Dude! User-Agent should always be added to, not overwritten.
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("appName"));
Okay, will modify tomorrow to do this right.
In this case, doesn't matter... But I agree with you
@darrelmiller happy? ๐
Thanks! This helped me with a demo I was putting together!
Thanks :-)
Awesome thanks, just needed this!
How to get the content from the github api? I am not able to extract content that I can see when I use cli to invoke the api
@Kriti021999 You will be able to get the content by adding the following after:
response.EnsureSuccessStatusCode();
string content = await response.Content.ReadAsStringAsync();
//todo: deserialize the JSON string in `content`content will have the JSON representation of the URL above. You may need to create the objects yourself. The goal of this API is when you need to create single-api calls without importing all the libraries. If you are making lots of GitHub API calls, you should use the SDK. ๐
@MaximRouiller Hi. The code snippet is only working for public repos. Can you help me figure out how I can access private repos as well?
Really appreciated, thanks!
This is the bare minimum to access the app. You could probably remove the "Accept" and rely on the default.
AuthorizationandUser-Agentare required.