LoginSignup
0
0

More than 3 years have passed since last update.

[備忘録] SAMLを使用するOrganization内のプルリクをGithub API v3でGETする(curl, Postman)

Last updated at Posted at 2020-04-14

GithubAPIを使ってやりたいことがあったのですが、認証でつまづいたのでメモ。
そもそもGithubAPIとはみたなところは下記参考にしたサイトをご参照ください。

参考にしたサイト

目的

  • 会社のリポジトリの自分のプルリク情報を手元でサクッとgetしたかっただけなのですが、OrganizationがSAMLを使用していたためちょっとつまづきました。そのあたりを自分用にまとめます。
  • 叩きたいAPIはコレ:GET /repos/:owner/:repo/pulls/:pull_number
    • :がついてるとこは変数

まずは認証なしでとれるやつ(SAMLとかじゃないパブリックなコンテンツ)

terminal

terminal
<Request>
curl https://api.github.com/repos/octokit/webhooks.js/pulls/122

<Response>
{
  "url": "https://api.github.com/repos/octokit/webhooks.js/pulls/122",
  "id": 402685424,
  "node_id": "MDExOlB1bGxSZXF1ZXN0NDAyNjg1NDI0",
  "html_url": "https://github.com/octokit/webhooks.js/pull/122",
  "diff_url": "https://github.com/octokit/webhooks.js/pull/122.diff",
  "patch_url": "https://github.com/octokit/webhooks.js/pull/122.patch",
  "issue_url": "https://api.github.com/repos/octokit/webhooks.js/issues/122",
  "number": 122,
  "state": "open",
  ...
}

Postman

  • MethodをGETにしてURLを入力してSendを押すだけ スクリーンショット 2020-04-14 12.39.29のコピー.png

認証いるやつ(今回のSAMLを使用したOrganizationのケース)

基本認証を使います(非推奨ですが簡単なので)

  1. 個人アクセストークンを作成する
  2. 個人アクセストークンをOrganizationで認可する
  3. 個人アクセストークンをパスワードにしてAPIにアクセスする([XXX]のところは各々読み替えをお願いします)

terminal

terminal
<Request>
① curl -u [username] https://api.github.com/repos/[hoge Organization]/[fuga Repository]/pulls/[1234]
   Enter host password for user [username]: [個人アクセストークン]
または
② curl -H 'Authorization: token [個人アクセストークン]' https://api.github.com/repos/[hoge Organization]/[fuga Repository]/pulls/[1234]

<Response>
{
  "url": "https://api.github.com/repos/octokit/webhooks.js/pulls/122",
  "id": 402685424,
  "node_id": "MDExOlB1bGxSZXF1ZXN0NDAyNjg1NDI0",
  "html_url": "https://github.com/octokit/webhooks.js/pull/122",
  "diff_url": "https://github.com/octokit/webhooks.js/pull/122.diff",
  "patch_url": "https://github.com/octokit/webhooks.js/pull/122.patch",
  "issue_url": "https://api.github.com/repos/octokit/webhooks.js/issues/122",
  "number": 122,
  "state": "open",
  ...
}
  • 注意点
    • ①は2020/11に使えなくなるらしいです。
    • サクッとやるなら基本認証で良いと思いますが、ちゃんとやるならOAuthでやった方が良さそうです。

Postman

  • 認証がない場合に加え、下記画像のAuthタブを選択し、TYPE=”Basic Auth”を選択
  • パスワードに個人アクセストークンを入力してSend

スクリーンショット 2020-04-14 12.53.15のコピー.png

最後に

ミス等ありましたらご指摘いただけますと幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0