[C#] Github 透過 Octokit.net 操控 Github - 取得所有 Repositories

2019-12-31

最近有一些需求,我要把檔案上傳到Github 上面,其實,有在看我部落格的朋友也知道,前一陣子我也把部落格的範例圖片都放到 Github 上面去了,當然我都是用網站手動上傳,最近因為有一些需求,我就想說乾脆研究一下,分成兩篇,第一篇取得所有的 Repositories 的 Id 和 名稱。


第一步 拿到 Github Personal access tokens ,你到這網址 https://github.com/settings/tokens 或是你透過你的Github 管理後台  設定 => Developer Settings => Personal access tokens 也可以找到,之後就新增一組,記得 repo 相關的都要開,完成後他只會出現一次,請記得要把他記下來等等程式碼中會用到。

第二步  拿到 Personal acces token 之後開一個專案並且透過 nuget 下載 Octokit 套件



第三步 列出 所有的 Repositories Name 還有  Id

try
{
//這 DONMATEST 可以任意都可以
var client = new GitHubClient(new ProductHeaderValue("DONMATEST"));
//從網站上取得的 personal access token https://github.com/settings/tokens
var tokenAuth = new Credentials(PersonalToken); // NOTE: not real token
client.Credentials = tokenAuth;
//下面那是你自己的 github name.
var repository = client.Repository.GetAllForUser("donma").Result;
foreach (var repo in repository)
{
//印出 id , name.
Console.WriteLine(repo.Id + ":" + repo.Name);
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write(ex.Message + "\r\n" + ex.StackTrace);
}




之後下一篇分享關於 上傳檔案上去,還有刪除原本的既有檔案

reference : https://octokitnet.readthedocs.io/en/latest/


當麻許的超技八 2014 | Donma Hsu Design.