C# example of downloading GitHub private repo programmatically

前端 未结 4 1119
广开言路
广开言路 2020-12-16 17:10

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

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

For a priva

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 17:42

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

提交回复
热议问题