アクセストークン、リフレッシュトークン、スコープ
OAuth2が発行するトークン、セッションを維持するリフレッシュトークン、トークンの操作範囲を制限するスコープについて理解します。
「アクセストークン、リフレッシュトークン、スコープ」はCoddyKit上の無料OAuth2 & OpenID Connect Deep Diveレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはOAuth2 & OpenID Connect Deep Dive学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 OAuth2 & OpenID Connect Deep Diveコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Tokens Are the Currency of OAuth2
After a successful flow, OAuth2 hands the client a token instead of the user's credentials. The client presents this token to the resource server to access protected data.
The Access Token
An access token is a short-lived credential proving the client may call an API on the user's behalf. It is sent on each request, usually in the Authorization header.
GET /api/profile
Authorization: Bearer eyJhbGciOi...Why Tokens Expire
Access tokens are deliberately short-lived (minutes to an hour). If one leaks, the window of misuse is small. But forcing the user to re-login constantly would be painful.
The Refresh Token
A refresh token is a longer-lived credential the client uses to obtain new access tokens without bothering the user. It is stored securely and never sent to APIs.
Refreshing in Practice
When the access token expires, the client posts the refresh token to the token endpoint and receives a fresh access token.
POST /oauth/token
grant_type=refresh_token
&refresh_token=def502...
&client_id=my-appWhat Scopes Are
Scopes declare exactly what a token may do, such as read:profile or write:orders. They implement least privilege at the token level.
scope=read:profile write:ordersRequesting Scopes
The client requests scopes during authorization. The user consents, and the issued token is limited to the granted scopes, no more.
GET /authorize?response_type=code
&client_id=my-app
&scope=read:profile
&redirect_uri=https://app/cbEnforcing Scopes
The resource server checks that the token carries the scope an endpoint requires. A token with only read:profile is rejected from a write endpoint.
if (!token.getScopes().contains("write:orders")) {
return forbidden();
}Reference vs Self-Contained
An access token may be a random reference the server looks up, or a self-contained JWT carrying claims the server validates by signature without a lookup.
Revoking Tokens
Refresh tokens can be revoked to end a session, for example on logout or suspected compromise. This invalidates the ability to mint new access tokens.
POST /oauth/revoke
token=def502...
&token_type_hint=refresh_tokenToken Storage Matters
Store tokens carefully: never in plain localStorage for sensitive apps, keep refresh tokens server-side or in secure storage, and always use HTTPS in transit.
Quick Check
An access token has expired but you do not want the user to log in again. What does the client use to get a new one?
Recap
You learned how OAuth2 tokens work:
- Access tokens are short-lived and sent to APIs
- Refresh tokens silently obtain new access tokens
- Scopes enforce least privilege per token
- Tokens can be reference-based or JWTs
- Revocation and secure storage protect sessions
These concepts underpin every OAuth2 grant type you will study next.
よくある質問
「アクセストークン、リフレッシュトークン、スコープ」レッスンは無料ですか?
はい。「アクセストークン、リフレッシュトークン、スコープ」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、OAuth2 & OpenID Connect Deep Diveコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 OAuth2 & OpenID Connect Deep Diveコースには全4レッスンが含まれています。
「アクセストークン、リフレッシュトークン、スコープ」で何を学びますか?
OAuth2が発行するトークン、セッションを維持するリフレッシュトークン、トークンの操作範囲を制限するスコープについて理解します。 ブラウザで直接実行するハンズオンコードでOAuth2 & OpenID Connect Deep Diveを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
OAuth2 & OpenID Connect Deep Diveを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのOAuth2 & OpenID Connect Deep Diveは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「アクセストークン、リフレッシュトークン、スコープ」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このOAuth2 & OpenID Connect Deep Diveレッスンでコードを書いて実行できますか?
はい。すべてのOAuth2 & OpenID Connect Deep Diveレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- OAuth2:委任プロトコル
- OAuth2の役割と用語
- 主要なGrant Typeの概要
- アクセストークン、リフレッシュトークン、スコープ