'Downloading Latest Release from a GitHub Repository using Octokit.net
How can I download the latest release of a private repository as a zip-file programmatically using Octokit.net?
I am not looking to download the entire repository, just the source code from a specified release, for example the latest one.
So far, I have created a GitHubClient, authorized with a GitHubToken, and I have gotten a list of all releases in the repo. I'm able to get information on the latest release, like the name, tag, Url, and ZipballUrl, etc.
I've attempted to download the release using the ZipballUrl, but it does not return a valid zip.
In my code below, I'm trying to download the latest release from the Octokit repo.
String workspaceName = "octokit";
String repositoryName = "octokit.net";
String filename = "temp.zip";
var client = new GitHubClient(new ProductHeaderValue(repositoryName));
String GitHubToken = "--- token goes here ---";
var tokenAuth = new Credentials(GitHubToken);
client.Credentials = tokenAuth;
// Retrieve a List of Releases in the Repository, and get latest using [0]-subscript
var releases = await client.Repository.Release.GetAll(workspaceName, repositoryName);
var latest = releases[0];
// Get a HttpResponse from the Zipball URL, which is then converted to a byte array
var response = await client.Connection.Get<object>(new Uri(latest.ZipballUrl), new Dictionary<string, string>(), "application/json");
byte[] releaseBytes = Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString());
// Create the resulting file using the byte array
await File.WriteAllBytesAsync(filename, releaseBytes);
I'm not very familiar with the GitHubClient, nor Http- or WebClients, so I cannot tell if the mistake lies in the HttpResponse and the content I get from there, or anywhere else in the file writing process.
Solution 1:[1]
To complement @haakonfp's own answer to this question I'd like to point out, for the GitHub API to not return a 403, a User-Agent header is needed in the request headers. You probably need a token only if you're trying to access a private repo, but in either case, said header will allow you to download the release contents.
var wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.UserAgent, "MyUserAgent");
wc.DownloadFile(latestVersion.ZipballUrl, "release.zip");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Maxi Jabase |
