TLS-based ChatGPT API with auto token regeneration, conversation tracking, proxy support and more.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Token management utilities for handling OpenAI authentication, including automatic login flow, token validation, manual token operations, and session management.
Utilities for checking and retrieving stored authentication tokens.
def token_expired() -> bool:
"""
Check if the stored access token has expired.
Returns:
bool: True if token is expired or missing, False if valid
Note:
Checks token stored in classes/auth.json file. Returns True if
file doesn't exist, token is malformed, or expiry time has passed.
"""def get_access_token() -> Tuple[str or None, str or None]:
"""
Retrieve stored access token and expiry time.
Returns:
Tuple[str or None, str or None]: (access_token, expires_at) or (None, None) if not found
Note:
Reads from classes/auth.json file. Returns None values if file
doesn't exist or is malformed.
"""from pychatgpt import OpenAI
# Check token status
if OpenAI.token_expired():
print("Token is expired or missing")
else:
token, expiry = OpenAI.get_access_token()
print(f"Token: {token[:20]}...")
print(f"Expires at: {expiry}")Handles the complete OpenAI authentication flow including login, captcha solving, and token creation.
class Auth:
"""
Manages OpenAI authentication through TLS-based login flow.
Args:
email_address (str): OpenAI account email address
password (str): OpenAI account password
proxy (str, optional): Proxy URL for network requests
"""
def __init__(
self,
email_address: str,
password: str,
proxy: str = None
): ...Perform the complete authentication flow to create a new access token.
def create_token(self):
"""
Execute the full authentication flow to create and save an access token.
This method handles the complex multi-step OpenAI login process including:
- Initial login page request
- CSRF token retrieval
- Auth0 authentication flow
- State token management
- Captcha handling (if required)
- Access token extraction and storage
Raises:
PyChatGPTException: If email/password not provided
Auth0Exception: If authentication steps fail
IPAddressRateLimitException: If IP is rate limited
Note:
If captcha is detected, saves captcha.png and prompts user for input.
Token is automatically saved to classes/auth.json on success.
"""from pychatgpt import OpenAI
# Create authentication handler
auth = OpenAI.Auth(
email_address="user@example.com",
password="your-password"
)
# Optional: use with proxy
auth = OpenAI.Auth(
email_address="user@example.com",
password="your-password",
proxy="http://proxy.example.com:8080"
)
try:
# Perform authentication flow
auth.create_token()
print("Authentication successful!")
# Token is now available
token, expiry = OpenAI.get_access_token()
print(f"Token created: {token[:20]}...")
except OpenAI.PyChatGPTException as e:
print(f"Authentication failed: {e.message}")Save access tokens manually for advanced use cases.
@staticmethod
def save_access_token(access_token: str, expiry: int or None = None):
"""
Manually save an access token with expiry time.
Args:
access_token (str): The access token to save
expiry (int, optional): Token expiry timestamp. Defaults to 1 hour from now.
Raises:
Exception: If file operations fail
Note:
Saves token to classes/auth.json file. If expiry is not provided,
sets expiry to current time + 3600 seconds (1 hour).
"""import time
from pychatgpt import OpenAI
# Save a token manually (advanced usage)
OpenAI.Auth.save_access_token(
access_token="your-token-here",
expiry=int(time.time()) + 3600 # Expires in 1 hour
)
# Verify it was saved
if not OpenAI.token_expired():
print("Token saved successfully")The authentication process involves these steps:
When a captcha is detected:
captcha.png in current directoryAll authentication requests support proxy configuration:
from pychatgpt import OpenAI
# String proxy
auth = OpenAI.Auth(
email_address="user@example.com",
password="password",
proxy="http://proxy.example.com:8080"
)
# Or as dictionary in Options when using Chat class
from pychatgpt import Chat, Options
options = Options()
options.proxies = {
"http": "http://proxy.example.com:8080",
"https": "https://secure-proxy.example.com:8443"
}
chat = Chat(email="user@example.com", password="password", options=options)Authentication can raise several exception types:
from pychatgpt import OpenAI
try:
auth = OpenAI.Auth("user@example.com", "password")
auth.create_token()
except OpenAI.PyChatGPTException as e:
print(f"General error: {e.message}")
except OpenAI.Auth0Exception as e:
print(f"Authentication error: {e.message}")
except OpenAI.IPAddressRateLimitException as e:
print(f"Rate limited: {e.message}")
print("Try again in a few minutes or use a proxy")Tokens are stored in classes/auth.json with this format:
{
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"expires_at": 1683847200
}The file is automatically created and managed by the authentication system.
Install with Tessl CLI
npx tessl i tessl/pypi-chatgptpy