Skip to main content
The OAuth 1.0a module provides authentication functionality for secure authorization flows, including request token generation, authorization URL generation, access token exchange, and OAuth1 signature generation.

OAuth1 Class

OAuth1 authentication handler for the X API.

Constructor

OAuth1(
    api_key: str,
    api_secret: str,
    callback: str,
    access_token: Optional[str] = None,
    access_token_secret: Optional[str] = None,
)
api_key
str
required
API Key (Consumer Key)
api_secret
str
required
API Secret (Consumer Secret)
callback
str
required
Callback URL for OAuth flow
access_token
str
default:"None"
Access Token (if already obtained)
access_token_secret
str
default:"None"
Access Token Secret (if already obtained)

Methods

get_request_token()

Get request token to start OAuth1 flow.
get_request_token() -> OAuth1RequestToken
Returns: OAuth1RequestToken with oauth_token and oauth_token_secret Raises:
  • ValueError - If request token retrieval fails

get_authorization_url()

Get the authorization URL for OAuth1 flow.
get_authorization_url(login_with_x: bool = False) -> str
login_with_x
bool
default:"False"
Whether to use “Log in with X” flow
Returns: Authorization URL string Raises:
  • ValueError - If request token not obtained. Call get_request_token() first.

get_access_token()

Exchange verifier for access token.
get_access_token(verifier: str) -> OAuth1AccessToken
verifier
str
required
OAuth verifier from callback or PIN
Returns: OAuth1AccessToken with access_token and access_token_secret Raises:
  • ValueError - If request token not obtained or access token exchange fails

start_oauth_flow()

Convenience method to start the OAuth1 flow.
start_oauth_flow(login_with_x: bool = False) -> str
login_with_x
bool
default:"False"
Whether to use “Log in with X” flow
Returns: Authorization URL string

build_request_header()

Build OAuth1 authorization header for API requests.
build_request_header(method: str, url: str, body: str = "") -> str
method
str
required
HTTP method (GET, POST, etc.)
url
str
required
Request URL (may include query parameters)
body
str
default:""
Request body (form-encoded string or empty)
Returns: OAuth1 authorization header string Raises:
  • ValueError - If access token not available. Complete OAuth1 flow first.

Example Usage

from xdk.oauth1_auth import OAuth1

# Initialize OAuth1
oauth = OAuth1(
    api_key="your_api_key",
    api_secret="your_api_secret",
    callback="http://localhost:3000/callback"
)

# Step 1: Start OAuth flow
auth_url = oauth.start_oauth_flow(login_with_x=True)
print(f"Visit this URL to authorize: {auth_url}")

# Step 2: User authorizes and you get the verifier from callback
verifier = "oauth_verifier_from_callback"

# Step 3: Exchange verifier for access token
access_token = oauth.get_access_token(verifier)
print(f"Access Token: {access_token.access_token}")
print(f"Access Token Secret: {access_token.access_token_secret}")

# Step 4: Use the access token to make API requests
header = oauth.build_request_header(
    "POST",
    "https://api.x.com/2/tweets",
    '{"text": "Hello World"}'
)

Using Existing Tokens

from xdk.oauth1_auth import OAuth1

# Initialize with existing tokens
oauth = OAuth1(
    api_key="your_api_key",
    api_secret="your_api_secret",
    callback="http://localhost:3000/callback",
    access_token="your_access_token",
    access_token_secret="your_access_token_secret"
)

# Build authorization header for requests
header = oauth.build_request_header(
    "GET",
    "https://api.x.com/2/users/me"
)

OAuth1RequestToken

OAuth1 request token response.
class OAuth1RequestToken:
    oauth_token: str
    oauth_token_secret: str

Attributes

oauth_token
str
The OAuth token
oauth_token_secret
str
The OAuth token secret

OAuth1AccessToken

OAuth1 access token response.
class OAuth1AccessToken:
    access_token: str
    access_token_secret: str

Attributes

access_token
str
The access token
access_token_secret
str
The access token secret

Build docs developers (and LLMs) love