使用C#以编程方式从私有GitHub存储库中读取图像 [英] Read image from private GitHub repository programmatically using C#

查看:85
本文介绍了使用C#以编程方式从私有GitHub存储库中读取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要使用C#将图像从私有git存储库存储到Blob.尝试使用以下代码,但出现404错误.

我正在使用以下代码

  1. 下载文件

有关更多详细信息,请参阅 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:

  1. 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 :

  1. Download file

For more details, please refer to https://developer.github.com/v3/repos/contents/#get-contents.

这篇关于使用C#以编程方式从私有GitHub存储库中读取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆