# OAuth 2.1 Quickstart — PietroCal

## 1. Registrar una aplicación

En PietroCal:

```text
Configuración → Integraciones y API → Aplicaciones OAuth
```

Elegí:

- **Pública** para aplicaciones móviles, desktop o navegador con PKCE.
- **Confidencial** para servidores capaces de proteger un Client Secret.

## 2. Crear PKCE

Generá:

- `code_verifier`: 43–128 caracteres.
- `code_challenge`: `BASE64URL(SHA256(code_verifier))`.

## 3. Abrir autorización

```text
GET /oauth/authorize.php
```

Parámetros:

```text
response_type=code
client_id=pc_oauth_...
redirect_uri=URI_EXACTA_REGISTRADA
scope=profile:read calendars:read
state=VALOR_ALEATORIO
code_challenge=...
code_challenge_method=S256
```

## 4. Intercambiar el código

```bash
curl -sS -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=authorization_code" \
  --data-urlencode "client_id=pc_oauth_..." \
  --data-urlencode "code=pc_code_..." \
  --data-urlencode "redirect_uri=http://localhost:8765/callback" \
  --data-urlencode "code_verifier=..." \
  "https://pietrocal.com/oauth/token.php"
```

## 5. Usar la API

```bash
curl -sS \
  -H "Authorization: Bearer pc_live_..." \
  -H "Accept: application/json" \
  "https://pietrocal.com/api/v1/me_app.php"
```

## 6. Renovar

```bash
curl -sS -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "grant_type=refresh_token" \
  --data-urlencode "client_id=pc_oauth_..." \
  --data-urlencode "refresh_token=pc_refresh_..." \
  "https://pietrocal.com/oauth/token.php"
```

El refresh token anterior queda consumido. Guardá siempre el nuevo.

## 7. Revocar

```bash
curl -sS -X POST \
  -H "Content-Type: application/x-www-form-urlencoded" \
  --data-urlencode "client_id=pc_oauth_..." \
  --data-urlencode "token=pc_refresh_..." \
  --data-urlencode "token_type_hint=refresh_token" \
  "https://pietrocal.com/oauth/revoke.php"
```

## Metadata

```text
https://pietrocal.com/.well-known/oauth-authorization-server/
```
