LoginSignup
28
16

More than 3 years have passed since last update.

OAuth 2.0 各社の仕様まとめ

Last updated at Posted at 2020-08-13

OAuth 2.0を利用したAPIを提供している各社サービスの気になる仕様をまとめました。
具体的には、ドキュメントやリクエストのURL、scopeの形、アクセストークンの有効期限、RFCで指定されていないオリジナルのパラメータなどです。RFCは自由度が高いので、どのように実装しているのか気になりますね👀

追加したい項目や修正など、編集リクエスト/コメントお願いします!

GitHub OAuth Apps

グラントタイプ: Authorization Code Grant

用途 URL
ドキュメント https://developer.github.com
OAuthクライアントの管理 https://github.com/settings/developers
認可リクエスト https://github.com/login/oauth/authorize
アクセストークンリクエスト https://github.com/login/oauth/access_token
リソースリクエスト https://api.github.com/user

認可リクエスト

URL: GET https://github.com/login/oauth/authorize

parameter description
response_type 不要。
client_id 必須。https://github.com/settings/developers で取得したものを指定する。
redirect_uri 省略可能。省略時は、https://github.com/settings/developers で設定したサイトにリダイレクトされる。
scope 省略可能。すべてではないが、「write:discussion」「read:discussion」など、「権限:リソース」の形をしているものが多い。ref.
state 省略可能。ランダムな文字列。
allow_signup
(original)
省略可能。ログインしていないユーザーに、OAuthフロー中にGi​​tHubにサインアップするオプションを提供するかどうかを指定する。true / false

認可レスポンス

codeとstateをパラメーターにしてredirect_uriにリダイレクトされる。
認可コードの有効期間は10分。

アクセストークンリクエスト

URL: POST https://github.com/login/oauth/access_token

parameter description
grant_type 不要。
code 必須。認可リクエストで取得した認可コードを指定する。
client_id 必須。https://github.com/settings/developers で取得したものを指定する。
client_secret
(original)
必須。https://github.com/settings/developers で取得したものを指定する。
redirect_uri 省略可能。承認後にユーザーが送信されるアプリケーションのURL。
state
(original)
省略可能。認可リクエストで指定したランダムな文字列。

アクセストークンレスポンス

access_token=e72e16c7e42f292c6912e7710c838347ae178b4a&token_type=bearer

expires_inがない。

リソースリクエスト

URL例: GET https://api.github.com/user
アクセストークンの指定: Authorizationヘッダーのtoken

YouTube Data API

ドキュメントサイトのURLを見る限りv3らしい。

グラントタイプ: Authorization Code Grant

用途 URL
ドキュメント https://developers.google.com/youtube/v3?hl=ja
OAuthクライアントの管理 https://code.google.com/apis/console?hl=ja
認可リクエスト https://accounts.google.com/o/oauth2/auth
アクセストークンリクエスト https://accounts.google.com/o/oauth2/token
リソースリクエスト https://www.googleapis.com/youtube/v3/channels

認可リクエスト

URL: GET https://accounts.google.com/o/oauth2/auth

parameter description
response_type 必須。codeを指定する。
client_id 必須。https://code.google.com/apis/console?hl=ja#access で取得したものを指定する。
redirect_uri 必須。https://code.google.com/apis/console?hl=ja#access で登録したものを指定する。
scope 必須。https://www.googleapis.com/auth/youtube.readonlyなど、URLの形をしている。特徴的。ref.
state 省略可能。
access_type
(original)
推奨。アプリケーションがユーザーなしにアクセストークンを更新できるかどうかを指定する。online / offline
approval_prompt
(original)
省略可能。ユーザーが特定の操作を実行する度に、アカウントのアクセス権をアプリケーションに付与するプロンプトを表示するかどうかを指定する。
login_hint
(original)
省略可能。ログインのヒントを提供するか指定する。

認可レスポンス

http://localhost/oauth2callback?code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf

アクセストークンリクエスト

URL: POST https://accounts.google.com/o/oauth2/token

parameter description
grant_type 必須。authorization_codeを指定する。
code 必須。認可リクエストで取得した認可コードを指定する。
redirect_uri 必須。https://code.google.com/apis/console?hl=ja#access で登録したものを指定する。
client_id 必須。https://code.google.com/apis/console?hl=ja#access で取得したものを指定する。
client_secret
(original)
必須。https://code.google.com/apis/console?hl=ja#access で取得したものを指定する。

アクセストークンレスポンス

{
  "access_token" : "ya29.AHES6ZTtm7SuokEB-RGtbBty9IIlNiP9-eNMMQKtXdMP3sfjL1Fc",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "1/HKSmLFXzqP0leUihZp2xUt3-5wkU7Gmu2Os_eBnzw74"
}

expires_inは3600で、1時間。

リソースリクエスト

URL例: GET https://www.googleapis.com/youtube/v3/channels?part=id&mine=true
アクセストークンの指定: Bearer(推奨) or クエリパラメータ

Twitter

OAuth 2.0以外にOAuth 1.0aなどの認証方法もあるが、今回はOAuth 2.0だけに注目する。
OAuth 2.0はClient Credentials Grantのみサポートされていて、publicな情報にしかアクセスできない。

グラントタイプ: Client Credentials Grant

用途 URL
ドキュメント https://developer.twitter.com
OAuthクライアントの管理 https://developer.twitter.com/en/apps
アクセストークンリクエスト https://api.twitter.com/oauth2/token
リソースリクエスト https://api.twitter.com/1.1/tweets/search/

アクセストークンリクエスト

URL: POST https://api.twitter.com/oauth2/token
クライアント認証: Basic認証

parameter description
grant_type 必須。client_credentialsを指定する。
scope 不要。publicな情報にしかアクセスできないためだと思われる。

アクセストークンレスポンス

HTTP/1.1 200 OK
Status: 200 OK
Content-Type: application/json; charset=utf-8
...
Content-Encoding: gzip
Content-Length: 140

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}

expires_inがなく、アクセストークンはユーザーが無効にするまで変わらない。publicな情報にしかアクセスできないためだと思われる。

リソースリクエスト

URL例: POST https://api.twitter.com/1.1/tweets/search/
アクセストークンの指定: Bearer

LINE Social API

LINEログインの仕組みを利用していて、OpenID Connectもサポートしている。

グラントタイプ: Authorization Code Grant (OpenID Connect含む)

用途 URL
ドキュメント https://developers.line.biz
OAuthクライアントの管理 https://developers.line.biz/console/
認可リクエスト https://access.line.me/oauth2/v2.1/authorize
アクセストークンリクエスト https://api.line.me/oauth2/v2.1/token
リソースリクエスト https://api.line.me/v2/profile

認可リクエスト

URL: GET https://access.line.me/oauth2/v2.1/authorize

parameter description
response_type 必須。codeを指定する。
client_id 必須。https://developers.line.biz/console/ で取得したものを指定する。
redirect_uri 必須。https://developers.line.biz/console/ で登録したものと同じでなければならない。
scope 必須。「openid」「profile%20openid%20email」など、%20で区切られている。ref.
state 必須。ランダムな文字列。
nonce 省略可能。
prompt 省略可能。
max_age 省略可能。
ui_locales 省略可能。
bot_prompt
(original)
省略可能。LINE公式アカウントを友だち追加するオプションをユーザーのログイン時に表示するかを指定する。normal / aggressive

認可レスポンス

HTTP/1.1 302 Found
Location : https://client.example.org/cb?code=abcd1234&state=0987poi&friendship_status_changed=true

認可コードの有効期間は10分。

アクセストークンリクエスト

URL: POST https://api.line.me/oauth2/v2.1/token

parameter description
grant_type 必須。authorization_codeを指定する。
code 必須。認可リクエストで取得した認可コードを指定する。
redirect_uri 必須。
client_id 必須。https://developers.line.biz/console/ で取得したものを指定する。
client_secret
(original)
必須。https://developers.line.biz/console/ で取得したものを指定する。

アクセストークンレスポンス

{
    "access_token": "bNl4YEFPI/hjFWhTqexp4MuEw5YPs...",
    "expires_in": 2592000,
    "id_token": "eyJhbGciOiJIUzI1NiJ9...",
    "refresh_token": "Aa1FdeggRhTnPNNpxr8p",
    "scope": "profile",
    "token_type": "Bearer"
}

expires_inは2592000で、30日間。

リソースリクエスト

URL例: GET https://api.line.me/v2/profile
アクセストークンの指定: Bearer

Google Sign-In for Websites

内部的にはOAuthを使っているが、JSファイルを読み込んで設定するだけで利用できるようになっている。解読するのは本稿の趣旨から外れるのでスキップ。

ドキュメント: https://developers.google.com/identity/sign-in/web
OAuthクライアントの管理: https://code.google.com/apis/console?hl=ja

Facebook Graph API

Facebook Login( https://developers.facebook.com/docs/facebook-login )のSDKが基本的に利用されいる。内部的にOAuth2.0が使われている。解読するのは本稿の趣旨から外れるのでスキップ。

ドキュメント: https://developers.facebook.com/docs/graph-api
OAuthクライアントの管理: https://developers.facebook.com/apps

28
16
3

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
28
16