使用C#以编程方式从私有GitHub存储库中读取图像 [英] Read image from private GitHub repository programmatically using C#
问题描述
需要使用C#将图像从私有git存储库存储到Blob.尝试使用以下代码,但出现404错误.
我正在使用以下代码
- 下载文件
有关更多详细信息,请参阅 https://developer.github.com/v3/repos/contents/#get-contents .
Need to store the image from a private git repository to a blob using C#. Tried with below code but getting 404 errors.
I am using the below code from C# example of downloading GitHub private repo programmatically
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);
}
Note: Able to fetch from the public repository
According to the message you provide, you use the wrong url to download. Regarding how to get the download url, please refer to the following steps:
- Use the following url to get the download url
Method: GET
URL: https://api.github.com/repos/:owner/:repo/contents/:path?ref:<The name of the commit/branch/tag>
Header:
Authorization: token <personal access token>
The repose body will tell you the download url For example :
- Download file
For more details, please refer to https://developer.github.com/v3/repos/contents/#get-contents.
这篇关于使用C#以编程方式从私有GitHub存储库中读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!