Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# example of downloading GitHub private repo programmatically

I see that the download path for a GitHub repo is of the form

https://github.com/{username}/{reponame}/archive/{branchname}.zip

For a private repo, understandably you need to provide credentials in order to download the repo, can anyone provide a C# example on how to provide a HTTPS basic authentication so I can download the repo programmatically?

Thanks,

like image 894
theburningmonk Avatar asked Mar 14 '13 11:03

theburningmonk


People also ask

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why is C programming language important?

Being a middle-level language, C reduces the gap between the low-level and high-level languages. It can be used for writing operating systems as well as doing application level programming. Helps to understand the fundamentals of Computer Theories.

Why is C called a mid level programming language?

C has the features of both assembly level languages i.e low-level languages and higher level languages. So that's why C is generally called as a middle-level Language. The user uses C language for writing an operating system and generates menu driven customer billing system.


2 Answers

See this guide on creating a personal access token then run the following:

var githubToken = "token";
var request = (HttpWebRequest)WebRequest.Create("https://api.github.com/repos/$OWNER/$REPO/contents/$PATH");
request.Headers.Add(HttpRequestHeader.Authorization, string.Concat("token ", githubToken));
request.Accept = "application/vnd.github.v3.raw";
request.UserAgent = "test app"; //user agent is required https://developer.github.com/v3/#user-agent-required
using (var response = request.GetResponse())
{
    var encoding = System.Text.ASCIIEncoding.UTF8;
    using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
    {
        var fileContent = reader.ReadToEnd();
    }
}
like image 124
Alex Radevich Avatar answered Oct 27 '22 11:10

Alex Radevich


Here is a pure C# way:

var githubToken = "[token]";
var url = "https://github.com/[username]/[repository]/archive/[sha1|tag].zip";
var path = @"[local path]";

using (var client = new System.Net.Http.HttpClient())
{
    var credentials = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}:", githubToken);
    credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", credentials);
    var contents = client.GetByteArrayAsync(url).Result;
    System.IO.File.WriteAllBytes(path, contents);
}
like image 42
Dinesh Bolkensteyn Avatar answered Oct 27 '22 12:10

Dinesh Bolkensteyn