[英]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?如何使用 Octokit.net 以编程方式将私有存储库的最新版本下载为 zip 文件?
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.到目前为止,我已经创建了一个 GitHubClient,使用 GitHubToken 进行授权,并且我已经获得了 repo 中所有版本的列表。 I'm able to get information on the latest release, like the name, tag, Url, and ZipballUrl, etc.
我能够获取有关最新版本的信息,例如名称、标签、Url 和 ZipballUrl 等。
I've attempted to download the release using the ZipballUrl, but it does not return a valid zip.我尝试使用 ZipballUrl 下载该版本,但它没有返回有效的 zip。
In my code below, I'm trying to download the latest release from the Octokit repo.在下面的代码中,我正在尝试从 Octokit 存储库下载最新版本。
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.我对 GitHubClient,也不是 Http- 或 WebClients 不是很熟悉,所以我无法判断错误是否出在 HttpResponse 和我从那里获得的内容,或者文件写入过程中的其他任何地方。
SOLVED:解决了:
I could not solve this entirely using the GitHubClient, so I am still interested to know if that is possible.我无法使用 GitHubClient 完全解决这个问题,所以我仍然很想知道这是否可能。
However, I managed to solve this by adding a WebClient to take care of the download after finding the ZipballUrl using Octokit.但是,在使用 Octokit 找到 ZipballUrl 后,我设法通过添加一个 WebClient 来处理下载来解决这个问题。 This had to be configured with User-Agent and Authorization headers to work.
这必须使用 User-Agent 和 Authorization 标头进行配置才能工作。
var latest = releases[0];
WebClient webClient = new WebClient();
webClient.Headers.Add("user-agent", "Anything");
webClient.Headers.Add("authorization", "token " + GitHubToken);
await webClient.DownloadFileTaskAsync(new Uri(latest.ZipballUrl), filename);
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.为了补充@haakonfp 自己对这个问题的回答,我想指出,对于 GitHub API 不返回 403,请求头中需要用户代理 Z099FB995346F31C749F6E40DB0F395。 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.
仅当您尝试访问私有存储库时,您可能才需要令牌,但无论哪种情况,header 都将允许您下载发布内容。
var wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.UserAgent, "MyUserAgent");
wc.DownloadFile(latestVersion.ZipballUrl, "release.zip");
Source: https://stackoverflow.com/a/37142247/10177659资料来源: https://stackoverflow.com/a/37142247/10177659
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.