0Pricing
OAuth2 & OpenID Connect Deep Dive · Lección

Tokens de acceso, tokens de actualización y scopes

Comprenda qué tokens emite OAuth2, cómo los tokens de actualización mantienen activas las sesiones y cómo los scopes limitan lo que puede hacer un token.

Tokens de acceso, tokens de actualización y scopes es una lección gratuita de OAuth2 & OpenID Connect Deep Dive en CoddyKit. Esta es la lección 4 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de OAuth2 & OpenID Connect Deep Dive, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de OAuth2 & OpenID Connect Deep Dive incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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-app

What 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:orders

Requesting 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/cb

Enforcing 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_token

Token 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.

Preguntas frecuentes

¿La lección «Tokens de acceso, tokens de actualización y scopes» es gratis?

Sí — el texto completo de «Tokens de acceso, tokens de actualización y scopes» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de OAuth2 & OpenID Connect Deep Dive, actualiza a CoddyKit PRO. El curso de OAuth2 & OpenID Connect Deep Dive incluye 4 lecciones en total.

¿Qué aprenderé en «Tokens de acceso, tokens de actualización y scopes»?

Comprenda qué tokens emite OAuth2, cómo los tokens de actualización mantienen activas las sesiones y cómo los scopes limitan lo que puede hacer un token. Practicas OAuth2 & OpenID Connect Deep Dive con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar OAuth2 & OpenID Connect Deep Dive?

No se requiere experiencia previa. OAuth2 & OpenID Connect Deep Dive en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 4 de 4.

¿Cuánto tiempo toma la lección «Tokens de acceso, tokens de actualización y scopes»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de OAuth2 & OpenID Connect Deep Dive?

Sí. Cada lección de OAuth2 & OpenID Connect Deep Dive incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. OAuth2: el protocolo de delegación
  2. Roles y terminología de OAuth2
  3. Introducción a los tipos de concesión
  4. Tokens de acceso, tokens de actualización y scopes
← Volver a OAuth2 & OpenID Connect Deep Dive