Skip to main content
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.

Parameters

path.base_url
str
default:"'https://api.x.com'"
path.authorization_base_url
str
default:"'https://x.com/i'"
path.client_id
str or None
default:"None"
path.client_secret
str or None
default:"None"
path.redirect_uri
str or None
default:"None"
path.token
Dict[str, Any] or None
default:"None"
path.scope
str or List[str] or None
default:"None"

__init__

Initialize the OAuth2 PKCE authentication.

Parameters

path.base_url
str
default:"'https://api.x.com'"
The base URL for the X API token endpoint (defaults to https://api.x.com).
path.authorization_base_url
str
default:"'https://x.com/i'"
The base URL for OAuth2 authorization (defaults to https://x.com/i).
path.client_id
str or None
default:"None"
The client ID for the X API.
path.client_secret
str or None
default:"None"
The client secret for the X API.
path.redirect_uri
str or None
default:"None"
The redirect URI for OAuth2 authorization.
path.token
Dict[str, Any] or None
default:"None"
An existing OAuth2 token dictionary (if available).
path.scope
str or List[str] or None
default:"None"
Space-separated string or list of strings for OAuth2 authorization scopes.

exchange_code

Exchange authorization code for tokens (matches TypeScript API).

Parameters

path.code
str
The authorization code from the callback.
path.code_verifier
str or None
default:"None"
Optional code verifier (uses stored verifier if not provided).

Returns

Dictstr, Any

fetch_token

Fetch token using authorization response URL (legacy method, uses exchange_code internally).

Parameters

path.authorization_response
str
The full callback URL received after authorization

Returns

Dictstr, Any

get_authorization_url

Get the authorization URL for the OAuth2 PKCE flow.

Parameters

path.state
str or None
default:"None"
Optional state parameter for security.

Returns

str

get_code_challenge

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

Returns

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]

Returns

str | None

is_token_expired

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

Returns

bool

refresh_token

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

Returns

Dictstr, Any

set_pkce_parameters

Manually set PKCE parameters.

Parameters

path.code_verifier
str
The code verifier to use.
path.code_challenge
str or None
default:"None"
Optional code challenge (will be generated if not provided).