> ## Documentation Index
> Fetch the complete documentation index at: https://docs.x.com/llms.txt
> Use this file to discover all available pages before exploring further.

# OAuth2PKCEAuth

> Reference for the oauth2_auth Python package in the X API SDK, grouping the client and Pydantic models for the oauth2 auth endpoints of the X API v2.

This module provides OAuth2 PKCE (Proof Key for Code Exchange) authentication
functionality for secure authorization flows. Includes code verifier generation,
token management, and automatic token refresh capabilities.

### `class xdk.oauth2_auth.OAuth2PKCEAuth`

OAuth2 PKCE authentication for the X API.

#### Parámetros

<ParamField path="path.base_url" type="str" default="'https://api.x.com'" />

<ParamField path="path.authorization_base_url" type="str" default="'https://x.com/i'" />

<ParamField path="path.client_id" type="str or None" default="None" />

<ParamField path="path.client_secret" type="str or None" default="None" />

<ParamField path="path.redirect_uri" type="str or None" default="None" />

<ParamField path="path.token" type="Dict[str, Any] or None" default="None" />

<ParamField path="path.scope" type="str or List[str] or None" default="None" />

### `__init__`

Initialize the OAuth2 PKCE authentication.

#### Parámetros

<ParamField path="path.base_url" type="str" default="'https://api.x.com'">
  The base URL for the X API token endpoint (defaults to [https://api.x.com](https://api.x.com)).
</ParamField>

<ParamField path="path.authorization_base_url" type="str" default="'https://x.com/i'">
  La URL base para la autorización OAuth2 (por defecto [https://x.com/i](https://x.com/i)).
</ParamField>

<ParamField path="path.client_id" type="str or None" default="None">
  The client ID for the X API.
</ParamField>

<ParamField path="path.client_secret" type="str or None" default="None">
  El client secret para la X API.
</ParamField>

<ParamField path="path.redirect_uri" type="str or None" default="None">
  La URI de redirección para la autorización OAuth2.
</ParamField>

<ParamField path="path.token" type="Dict[str, Any] or None" default="None">
  An existing OAuth2 token dictionary (if available).
</ParamField>

<ParamField path="path.scope" type="str or List[str] or None" default="None">
  Cadena separada por espacios o lista de cadenas para los scopes de autorización OAuth2.
</ParamField>

### `exchange_code`

Intercambia el código de autorización por tokens (coincide con la API de TypeScript).

#### Parámetros

<ParamField path="path.code" type="str">
  El código de autorización del callback.
</ParamField>

<ParamField path="path.code_verifier" type="str or None" default="None">
  Verificador de código opcional (usa el verificador almacenado si no se proporciona).
</ParamField>

#### Devuelve

`Dictstr, Any`

### `fetch_token`

Fetch token using authorization response URL (legacy method, uses exchange\_code internally).

#### Parámetros

<ParamField path="path.authorization_response" type="str">
  The full callback URL received after authorization
</ParamField>

#### Devuelve

`Dictstr, Any`

### `get_authorization_url`

Obtiene la URL de autorización para el flujo OAuth2 PKCE.

#### Parámetros

<ParamField path="path.state" type="str or None" default="None">
  Parámetro de estado opcional por seguridad.
</ParamField>

#### Devuelve

`str`

### `get_code_challenge`

Get the current code challenge (for PKCE).
:returns: The current code challenge, or None if not set.
:rtype: Optional\[str]

#### Devuelve

`str | None`

### `get_code_verifier`

Get the current code verifier (for PKCE).
:returns: The current code verifier, or None if not set.
:rtype: Optional\[str]

#### Devuelve

`str | None`

### `is_token_expired`

Check if the token is expired.
:returns: True if the token is expired, False otherwise.
:rtype: bool

#### Devuelve

`bool`

### `refresh_token`

Refresh the access token.
:returns: The refreshed token dictionary
:rtype: Dict\[str, Any]

#### Devuelve

`Dictstr, Any`

### `set_pkce_parameters`

Manually set PKCE parameters.

#### Parámetros

<ParamField path="path.code_verifier" type="str">
  The code verifier to use.
</ParamField>

<ParamField path="path.code_challenge" type="str or None" default="None">
  Optional code challenge (will be generated if not provided).
</ParamField>
