How to Post to Reddit Using Python

 

In this tutorial, we’ll walk through how to automate posting to Reddit using Python. This guide is designed for beginners looking to interact with Reddit’s API programmatically, whether for personal use, bot creation, or automating your social media workflows. By the end, you’ll be able to post to a subreddit with a script that handles authentication.

1. Create a Reddit App

To interact with Reddit’s API, you’ll need to register an app. Here’s how to do it:

  1. Go to the Reddit Developer Console: Head to reddit.com/prefs/apps.
  2. Click ‘Create App’: At the bottom of the page, find the “Create App” button.
  3. Fill in the App Details:
  • Name: Choose a descriptive name.
  • App Type: Select ‘script’ since we’ll be using this for a single user.
  • Description: Optional, but you can leave this blank.
  • Redirect URI: Set this to http://localhost:8080 or any placeholder URI. We do not need this now, but in a later tutorial we will use this to enable a more secure form of authentication for our app called OAuth, which requires a redirect URL.
  • Personal Use Script (Client ID) and Secret: After creating the app, you’ll get these keys. Keep them safe!

Here is an example of what I filled in down below.

2. Install the Required Libraries

In your Python environment, install the praw library, which is used for interacting with Reddit's API. You should have pip on your computer if you have been working with Python.

 

pip install praw

3. Write the Python Script

Now let’s create the Python script that will authenticate and post content to Reddit.


import praw

# Reddit API credentials
reddit = praw.Reddit(
client_id='your_client_id',
client_secret='your_client_secret',
user_agent='your_app_name',
username='your_reddit_username',
password='your_reddit_password'
)
# Subreddit to post to
subreddit_name = "test" # Change to your desired subreddit
# Title and content of the post
title = "My first automated post using Python!"
selftext = "Hello Reddit! This post was made using a Python script."
# Submit the post
subreddit = reddit.subreddit(subreddit_name)
subreddit.submit(title=title, selftext=selftext)
print(f"Posted to Reddit: {title}")

4. Explanation of the Script

  • praw: This is the Python library that simplifies Reddit API interactions.
  • Reddit API credentials: Replace the placeholders with your app’s credentials (Client ID, Client Secret, etc.).

Here is an example of an app with all of the credentials. You can see the client id is under “personal use script”., quite long and random.

You can place the username and password of your reddit account in there as well. FYI this method I am showing you will not work with two-factor, if you are interested leave a comment and we can work on a more advanced methodology. For now you will have to disable two factor auth on your account to get this to work.

Finally you should place the secret in the secret section and for the user agent just put the name of the app! In my case that would be test-shilleh

  • Subreddit: Define the subreddit you want to post to. You can use a test subreddit like r/test for practice.
  • Submit the post: The submit() method handles posting to the subreddit with a title and a body (selftext). For link posts, you can pass a urlinstead of selftext.

5. Run the Script

Once you’ve set up everything, simply run your Python script:

 

python post_to_reddit.py

If everything is set up correctly, you should see your post in the subreddit you specified!

Conclusion

Automating your Reddit posts can streamline your workflow and save you time. In this tutorial, we walked through how to create a Reddit app, use praw to authenticate, and post to a subreddit with a simple script. Feel free to extend this by adding more advanced features like fetching data from an API or scheduling your posts.

If you found this tutorial helpful, consider subscribing for more Python and automation content!

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Simple Guide: Build a Reverse Geolocator with Raspberry Pi Pico W and GPS Module

Simple Guide: Build a Reverse Geolocator with Raspberry Pi Pico W and GPS Module

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.