3つの方法がある。
対象環境
認証方法
公式サイトを見ると主に3つの方法がある。
認証方式 | コマンド | 必要なもの |
---|---|---|
Basic認証 | curl -u "username:password" https://api.github.com |
username, password |
OAuth2 Token | curl https://api.github.com/?access_token=OAUTH-TOKEN |
OAUTH-TOKEN |
OAuth2 Key/Secret | curl 'https://api.github.com/users/whatever?client_id=xxxx&client_secret=yyyy' |
client_id, client_secret |
こちらの方がわかりやすく説明されている。 私はバッチファイルで実行することを想定している。Webアプリについては考えていない。
必要なものの入手方法
username, password
GitHubにSin upしてアカウントを作成する。 このとき、username, password を作成する。
ログインにも使うので書き留めておく。
access token
- GitHubへログインする
- プロフィール設定へ移動する。
- Personal access tokensページへ移動する
Token description
に任意の文字列を入力するSelect scopes
でrepo
にチェックするGenalate new token
ボタンを押下して作成する- 表示された文字の羅列が access token である
- アイコンをマウスオーバーすると
Copy Token
と出る - クリックする
- access token をどこかにペーストする(または書き留めておく)
- なお、access tokenは1度でもページ更新すると二度と表示されない
access tokenを忘れてしまった場合
Edit
してRegenerate token
で再発行するしかない。
たぶん以前までのaccess tokenは使えなくなると思う。
- GitHubへログインする
- プロフィール設定へ移動する。
- Personal access tokensページに遷移する
- 対象Tokenの
Edit
ボタンを押下する Regenerate token
ボタンを押下する- 発行された access token をどこかに書き留めておく
scope
GitHubAPIのscopeを参照。
ようするに、そのaccess tokenの操作権限を設定する。repo
にチェックしておけばリモートリポジトリの取得や生成ができる。削除までしたければdelete_repo
にもチェックする。
でも、もしaccess tokenが漏洩したら、リポジトリを勝手に削除されてしまいかねない。
client_id, client_secret
- GitHubへログインする
- プロフィール設定へ移動する。
- OAuth Applicationsページに遷移する
Register a new application
ボタンを押下する- 任意の
Application name
,Homepage URL
,Authorization callback URL
を入力する
- 作成したらOAuth Applicationsページに戻ってくる
- そこでApplicationNameのリンクが表示される
- クリックする
Client ID
,Client Secret
が表示される。それをどこかに書き留めておく。
認証テスト
- msys.batを起動する
以後、ここでcurlコマンドを実行する。
Basic認証
ユーザ名とパスワード
以下の値と仮定する。
ユーザ名 | パスワード |
---|---|
yourUsername | yourPassword |
コマンド
curl -u "yourUsername:yourPassword" https://api.github.com/user
応答
idなど一部の値は適当にマスクした。
{
"login": "yourUsername",
"id": 01234567,
"avatar_url": "https://avatars.githubusercontent.com/u/01234567?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/yourUsername",
"html_url": "https://github.com/yourUsername",
"followers_url": "https://api.github.com/users/yourUsername/followers",
"following_url": "https://api.github.com/users/yourUsername/following{/other_user}"
,
"gists_url": "https://api.github.com/users/yourUsername/gists{/gist_id}",
"starred_url": "https://api.github.com/users/yourUsername/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/yourUsername/subscriptions",
"organizations_url": "https://api.github.com/users/yourUsername/orgs",
"repos_url": "https://api.github.com/users/yourUsername/repos",
"events_url": "https://api.github.com/users/yourUsername/events{/privacy}",
"received_events_url": "https://api.github.com/users/yourUsername/received_events",
"type": "User",
"site_admin": false,
"name": null,
"company": null,
"blog": null,
"location": null,
"email": null,
"hireable": null,
"bio": null,
"public_repos": 15,
"public_gists": 0,
"followers": 0,
"following": 0,
"created_at": "2016-01-01T00:00:00Z",
"updated_at": "2016-01-23T00:00:00Z",
"private_gists": 0,
"total_private_repos": 0,
"owned_private_repos": 0,
"disk_usage": 571,
"collaborators": 0,
"plan": {
"name": "free",
"space": 976562499,
"collaborators": 0,
"private_repos": 0
}
}
OAuth2 Token認証
AccessToken
AccessTokenは0123456789abcdefghijklmnopqrstuvwxyz0123
と仮定する。
コマンド
curl "https://api.github.com/user?access_token=0123456789abcdefghijklmnopqrstuvwxyz0123
応答
Basic認証の結果とおなじものが返ってくる。
OAuth2 Key/Secret認証
client_id, client_secret
client_id | client_secret |
---|---|
0123456789abcdefghij | abcdefghijklmnopqrstuvwxyz0123456789abcd |
コマンド
curl "https://api.github.com/user?client_id=0123456789abcdefghij&client_secret=abcdefghijklmnopqrstuvwxyz0123456789abcd"
{
"message": "Requires authentication",
"documentation_url": "https://developer.github.com/v3"
}
どうやらhttps://api.github.com/user
APIはこれでは認証できないらしい。
以下のコマンドにすると応答した。
curl "https://api.github.com/users/whatever?client_id=0123456789abcdefghij&client_secret=abcdefghijklmnopqrstuvwxyz0123456789abcd"
応答
idなど一部の値は適当にマスクした。
{
"login": "whatever",
"id": 01234,
"avatar_url": "https://avatars.githubusercontent.com/u/01234?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/whatever",
"html_url": "https://github.com/whatever",
"followers_url": "https://api.github.com/users/whatever/followers",
"following_url": "https://api.github.com/users/whatever/following{/other_user}
",
"gists_url": "https://api.github.com/users/whatever/gists{/gist_id}",
"starred_url": "https://api.github.com/users/whatever/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/whatever/subscriptions",
"organizations_url": "https://api.github.com/users/whatever/orgs",
"repos_url": "https://api.github.com/users/whatever/repos",
"events_url": "https://api.github.com/users/whatever/events{/privacy}",
"received_events_url": "https://api.github.com/users/whatever/received_events"
,
"type": "User",
"site_admin": false,
"name": null,
"company": null,
"blog": null,
"location": null,
"email": null,
"hireable": null,
"bio": null,
"public_repos": 1,
"public_gists": 0,
"followers": 3,
"following": 0,
"created_at": "2016-01-01T00:00:00Z",
"updated_at": "2016-01-23T00:00:00Z"
}
一長一短
Basic認証
最も簡単。
でも、パスワードを直接扱うため危険。 漏洩したらアカウントが乗っ取られる。 リポジトリの意図せぬ削除、なりすまして発言されるなどが起こりうる。
AccessToken認証
最も安全。
AccessTokenが漏洩しても、再発行(Regenerate token)で無効化できる。と思う。未確認。
パスワードの変更と同じ効果がある。 ボタン一発でできるので簡単。
また、Scopeの設定で操作を制限できる。 万一漏洩していたずらされても影響範囲を抑えることができる。
client_id, client_secret認証
よくわからん。この方法は必要がない限り使わなくていいか。
client_id, client_secretはuser取得ができなかった。 コマンドに応じて認証方法が違うのかもしれない。
所感
AccessTokenで認証するのが安全そう。