Skip to content

Instantly share code, notes, and snippets.

@MaximRouiller
Last active March 27, 2024 22:45
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MaximRouiller/74ae40aa994579393f52747e78f26441 to your computer and use it in GitHub Desktop.
Save MaximRouiller/74ae40aa994579393f52747e78f26441 to your computer and use it in GitHub Desktop.
GitHub API access with Personal Access Token using C# HttpClient and .NET Core
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");
}
}
@cdiggins
Copy link

Really appreciated, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment