gpt4 book ai didi

以编程方式下载 GitHub 私有(private)存储库的 C# 示例

转载 作者:太空狗 更新时间:2023-10-29 17:50:51 25 4
gpt4 key购买 nike

我看到 GitHub 存储库的下载路径是这样的

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

对于私有(private)仓库,可以理解您需要提供凭据才能下载仓库,谁能提供一个 C# 示例来说明如何提供 HTTPS 基本身份验证,以便我可以通过编程方式下载仓库?

谢谢,

最佳答案

这是一种纯 C# 方式:

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

关于以编程方式下载 GitHub 私有(private)存储库的 C# 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15408053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com