C++ Builder CE 中級プログラミング

GitHubAPIを使って、リモートレポジトリの一覧を得る 1.準備編

だいぶ前にGitHubのアカウントを作って、アクセストークンも得ていたので、それを使います。以下を参考にしてください。

ユーザ名 alt-doc-naoですがcurlコマンドでは使いません。ユーザ名はPATに織り込まれています。
PAT ghp_5Kz5tXXXN8Q9jH7id6CYYYUmF

として、このユーザのレポジトリの一覧をhttpsのgetで得るには、curlのコマンドで、

curl --header "Authorization: bearer ghp_use_your_own_pat_here" "https://api.github.com/user/repos"

で得られるはずです。試して見ましょうかね。

Windowsのコマンドプロンプトから、

C:\Users\jakeb>curl –header “Authorization: bearer ghp_5Kz5tGOGfXXXYYYZZZ” “https://api.github.com/user/repos”

以下が得られますが、

[
{
“id”: 961590475,
“node_id”: “R_kgDOOVC0yw”,
“name”: “bootnext”,
“full_name”: “alt-doc-nao/bootnext”,
“private”: false,
“owner”: {
“login”: “alt-doc-nao”,
“id”: 189937208,
“node_id”: “U_kgDOC1I2OA”,
“avatar_url”: “https://avatars.githubusercontent.com/u/189937208?v=4”,
“gravatar_id”: “”,
“url”: “https://api.github.com/users/alt-doc-nao”,
“html_url”: “https://github.com/alt-doc-nao”,
“followers_url”: “https://api.github.com/users/alt-doc-nao/followers”,
“following_url”: “https://api.github.com/users/alt-doc-nao/following{/other_user}”,
“gists_url”: “https://api.github.com/users/alt-doc-nao/gists{/gist_id}”,
“starred_url”: “https://api.github.com/users/alt-doc-nao/starred{/owner}{/repo}”,
“subscriptions_url”: “https://api.github.com/users/alt-doc-nao/subscriptions”,
“organizations_url”: “https://api.github.com/users/alt-doc-nao/orgs”,
“repos_url”: “https://api.github.com/users/alt-doc-nao/repos”,
“events_url”: “https://api.github.com/users/alt-doc-nao/events{/privacy}”,
“received_events_url”: “https://api.github.com/users/alt-doc-nao/received_events”,
“type”: “User”,
“user_view_type”: “public”,
“site_admin”: false
},
“html_url”: “https://github.com/alt-doc-nao/bootnext”,
“description”: “switch disk using bootnext i.e. bootonce”,
“deployments_url”: “https://api.github.com/repos/alt-doc-nao/bootnext/deployments”,
“created_at”: “2025-04-06T20:25:49Z”,
“updated_at”: “2025-04-06T23:55:44Z”,
“pushed_at”: “2025-04-06T23:55:40Z”,

以下は省略しました。

このように膨大な情報が得られますが、今捉えたいのは例えば下記の項目なので、

.name
.private
.html_url
.description
.updated_at

jqにパイプしてなんとかしてみましょうかね。例えば、

$ curl -s --header "Authorization: bearer ghp_invalid_access_token_use_your_own_token_here" "https://api.github.com/user/repos" | jq -r '.[] | .name, .private, .description, .html_url, .updated_at'
bootnext
  false
  switch disk using bootnext i.e. bootonce
  oopshttps://github.com/alt-doc-nao/bootnext
  2025-04-06T23:55:44Z
First_repository
  true
  null
  oopshttps://github.com/alt-doc-nao/First_repository
  2024-12-02T07:13:59Z
hello-world
  false
  yet another "hello, world" program using tmemo
  oopshttps://github.com/alt-doc-nao/hello-world
  2024-12-03T05:44:35Z
some-new-repo
  false
  TMemo繧剃スソ縺」縺ヲ窶昴%繧薙↓縺。縺ッ縲∽ク也阜窶昴r陦ィ遉コ
  oopshttps://github.com/alt-doc-nao/some-new-repo
  2024-12-03T07:08:27Z

項目の階層が同じなので、これで取れそうですね。ちなみにコマンドプロンプトからだと引用符が無駄に効いて上手く動かない可能性がありますので、Open Git Bash hereからBashを使いました。またurlに余計な解釈が入るので、oopsを足してキャンセルしました。次でC++ Builder CEからhttps getして、得られたJSONを処理します。最終的にはTListViewに結果を示す予定です。

コメント