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 Secret (Consumer Secret)
Callback URL for OAuth flow
Access Token (if already obtained)
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
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
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
Whether to use “Log in with X” flow
Returns: Authorization URL string
Build OAuth1 authorization header for API requests.
build_request_header(method: str, url: str, body: str = "") -> str
HTTP method (GET, POST, etc.)
Request URL (may include query parameters)
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
OAuth1AccessToken
OAuth1 access token response.
class OAuth1AccessToken:
access_token: str
access_token_secret: str
Attributes