11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【OAuth】Github APIの制限を緩和する ~「Client ID」「Client Secret」の取得方法

Last updated at Posted at 2022-04-04

Github API制限について

GithubでAPIを使用する場合、同一IPからのアクセスは一時間に60回までの制限があります。

何も考えずにAPIを叩きまくると、すぐに制限を超えて以下のJSONが返されます。

zsh
{"message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)", "documentation_url": "https://developer.github.com/v3/#rate-limiting"}

この制限を緩和するには、Authenticatedなリクエストが必要となります。
緩和後は5000 requests/hourまでAPIでのアクセスが可能になります。

OAuth設定

この記事では上記の緩和に必要なOAuthの設定を行います。
取得可能な情報は以下の2つです。

  • Client ID
  • Client Secret

既にGithubのアカウントは取得しているものとして、以下で設定していきます。

取得までの流れ

スクリーンショット 2022-04-04 19.19.09.png

まずはGithubにsign inして、右上のユーザーアイコンからSettingsを開きます。

スクリーンショット 2022-04-04 19.13.46.png

Setting画面左のNavigationからDevelopper settingsを開きます。

スクリーンショット 2022-04-04 19.23.53.png

GitHub Apps画面に遷移するので、続いてOAuth Appsを選択します。

スクリーンショット 2022-04-04 19.25.35.png

OAuth Appを新規登録するので、New OAuth Appをクリックします。

スクリーンショット 2022-04-04 19.29.43.png

必要情報を記載します。
Web アプリでの GitHub 認証時には、ここで設定したアプリ名がユーザーに提示されることになります。 怪しいアプリだと思われないように、アプリ名はちゃんとしたものを設定しましょう
(私はGithubActionsでAPIを使用したかったため、HomepageURLをgithubのリポジトリにしていますが、各自適切なものに変更が必要です。)

記載したらRegister applicationをクリックします。

スクリーンショット 2022-04-04 19.35.46.png

Client IDが発行されています。

続いてClient Secretも発行したいので、Generate a new client secretをクリックします。

スクリーンショット 2022-04-04 19.39.39.png

無事Client Secretsも発行されました。
Regenerateも可能ではありますが、Secretsの情報自体は画面遷移してしまうと確認できなくなりますので、保存必須です

Github APIを上限緩和させるためのquery

上記の方法でclient関連のkeyを取得したら、APIにqueryを付け足せばアクセス数は上限緩和されます。
例えば自分の各リポジトリを取得するAPIを叩くのであれば以下の通りです。

https://api.github.com/users/"<githubのusername>"/repos?&client_id="<Client ID>"&client_secret="<Client Secrets>"

まとめ

1時間で60件の上限はかなり少ない印象なので、APIを使うなら上記の設定は必須に思える。
追記あればまた更新します!!

11
6
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
11
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?