Prawcore
Prawcore is a low-level communication layer designed to be used by PRAW (Python Reddit API Wrapper) version 4 and above. It handles the underlying HTTP requests and authentication mechanisms for interacting with the Reddit API, abstracting away the complexities of direct API communication. The library is actively maintained, currently at version 3.0.2, with releases typically tied to PRAW updates or significant changes in the Reddit API.
Warnings
- breaking Prawcore explicitly follows semantic versioning where only major versions introduce breaking changes to its public interface. Always pin your major version dependency to avoid unexpected breakages.
- breaking Prawcore 3.0.0 dropped support for Python 3.8. Earlier versions also dropped support for Python 3.6 and 3.7. Ensure your Python environment meets the `~=3.9` requirement.
- breaking In prawcore 3.0.0, the `RateLimiter` attribute `next_request_timestamp` was removed and replaced with `next_request_timestamp_ns`.
- gotcha Applications frequently encounter `prawcore.exceptions.OAuthException: invalid_grant`. This usually indicates an issue with your OAuth credentials (Client ID, Client Secret, Username, Password) or an active 2FA on the Reddit account preventing script access.
- gotcha Encountering `prawcore.exceptions.Forbidden` (HTTP 403) means the authenticated user lacks permission for the requested action or resource, such as accessing private subreddits or replying to locked content.
- gotcha Older versions of prawcore had issues handling Reddit's rate limits, leading to frequent 429 (Too Many Requests) errors. While newer versions improved this, robust error handling for `prawcore.exceptions.ResponseException` and `prawcore.exceptions.RequestException` is crucial for stable bots.
Install
-
pip install prawcore
Imports
- prawcore
import prawcore
- TrustedAuthenticator
from prawcore import TrustedAuthenticator
- Requestor
from prawcore import Requestor
- ReadOnlyAuthorizer
from prawcore import ReadOnlyAuthorizer
- session
from prawcore import session
Quickstart
import os
import pprint
import prawcore
# Ensure these environment variables are set before running:
# PRAWCORE_CLIENT_ID, PRAWCORE_CLIENT_SECRET
client_id = os.environ.get('PRAWCORE_CLIENT_ID', '')
client_secret = os.environ.get('PRAWCORE_CLIENT_SECRET', '')
user_agent = "YOUR_VALID_USER_AGENT"
if not client_id or not client_secret:
print("Error: PRAWCORE_CLIENT_ID and PRAWCORE_CLIENT_SECRET environment variables must be set.")
exit(1)
authenticator = prawcore.TrustedAuthenticator(
prawcore.Requestor(user_agent),
client_id,
client_secret,
)
authorizer = prawcore.ReadOnlyAuthorizer(authenticator)
authorizer.refresh()
with prawcore.session(authorizer) as session:
try:
response = session.request("GET", "/api/v1/user/bboe/trophies")
pprint.pprint(response)
except prawcore.exceptions.ResponseException as e:
print(f"API Error: {e.response.status_code} - {e.response.text}")
except prawcore.exceptions.RequestException as e:
print(f"Request Error: {e}")