Prawcore

3.0.2 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This example demonstrates how to use prawcore to fetch a user's trophies. It assumes you have created a Reddit script application and set `PRAWCORE_CLIENT_ID` and `PRAWCORE_CLIENT_SECRET` environment variables. Replace 'YOUR_VALID_USER_AGENT' with a unique and descriptive user agent string.

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}")

view raw JSON →