我正在使用 Octokit.net 0.9.0 版
(GitHub API for .NET),用于获取少数存储库的 zip 内容。
我已经有了我需要的存储库列表,但我无法将存储库的内容作为 .zip 文件(称为 zipball)获取
到目前为止我尝试了什么
// ... client = new Client(...);
// some authentication logic...
// some other queries to GitHub that work correctly
var url = "https://api.github.com/repos/SomeUser/SomeRepo/zipball";
var response = await this.client.Connection.Get<byte[]>(
new Uri(url),
new Dictionary<string, string>(),
null);
var data = response.Body;
var responseData = response.HttpResponse.Body;
我的尝试有问题
数据
为空responseData.GetType().Name
表示responseData
是字符串类型- 当我尝试
Encoding.ASCII.GetBytes(response.HttpResponse.Body.ToString());
我得到无效的 zip 文件
问题
在使用 Octokit.net 库进行身份验证后获取存储库 zipball 的正确方法是什么?
我也打开了an issue在 octokit.net 存储库中。
最佳答案
在检查了 Octokit 的源代码后,我认为这是不可能的(从版本 0.10.0 开始):
参见 Octokit\Http\HttpClientAdapter.cs
// We added support for downloading images. Let's constrain this appropriately.
if (contentType == null || !contentType.StartsWith("image/"))
{
responseBody = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
}
else
{
responseBody = await responseMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}
响应主体 (responseData
) 被转换为一个 unicode 字符串,因此,它被破坏了,而不是二进制的。看我的PR792 (需要将 if 语句替换为 if (contentType == null || (!contentType.StartsWith("image/") && !contentType.StartsWith("application/")))
)来修复这(然后它是一个字节数组。可以使用 System.IO.File.WriteAllBytes("c:\\test.zip", (byte[])responseData);
编写)。
关于c# - 无法在 Octokit.net 中将存储库内容作为 .zip 文件 (zipball),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29766916/